重定向URI与DeferredResult <responseentity>不兼容

时间:2018-10-23 11:25:19

标签: java spring asynchronous spring-async

下面是我用于对此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错误408(此页面无效)

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

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

1 个答案:

答案 0 :(得分:0)

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

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