下面是我用来响应此 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?