1

下面是我用来响应此 API 的代码示例。

@RequestMapping(value = "/transactionStatusAsync")
public DeferredResult<ResponseEntity<?>> asyncTransactionStatus(HttpServletRequest request, Model model,
        Locale locale, HttpServletResponse response) {
    LOGGER.info("Request received async-deferredResult TransactionStatus request");
    //return generateDeferredAndProcessRequest(request, response);
    DeferredResult<ResponseEntity<?>> output = new DeferredResult<>();
    output.setResult(createResponseEntity(ERROR_PAGE,
            HttpStatus.REQUEST_TIMEOUT));
    return output ;
}

private static ResponseEntity<?> createResponseEntity(String path, HttpStatus status) {
    URI location = URI.create(path); **// "/WEB-INF/views/jsp/error.jsp"**
    boolean absolute = location.isAbsolute(); **//false**
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setLocation(location);
    ResponseEntity<?> objectResponseEntity = new ResponseEntity<>(responseHeaders, status);
    return objectResponseEntity ;
}

注意:URI 是相对的,不是绝对的 URI。

错误出现:HTTP ERROR 408(此页面不工作)

虽然,当我在响应正文中写入字节时,我对DeferredResult 的响应工作正常,如下所示: output.setResult(ResponseEntity.status(HttpStatus.OK).body(responseData.getBytes()));

我应该怎么做才能在延迟响应中回复特定的 URI?

4

1 回答 1

0

HttpStatus.REQUEST_TIMEOUT是 HTTP 错误代码 408。location标头仅用于 3xx(重定向)或 201(已创建)状态响应。

即,您需要将状态更改为HttpStatus.FOUND(302) 才能使您的代码正常工作。

于 2020-05-21T08:36:28.363 回答