简体   繁体   English

Spring 3.2 DeferredResult - 如何设置错误响应的状态代码?

[英]Spring 3.2 DeferredResult - How to set status code for error response?

Spring Web 3.2 comes with a DeferredResult class for asynchronous request processing. Spring Web 3.2附带了一个用于异步请求处理的DeferredResult类。 It has a setErrorResult for providing an alternative response if something goes wrong, but no option to supply a http error code. 它有一个setErrorResult用于在出现问题时提供备用响应,但没有提供http错误代码的选项。

Surely it must be possible to control the http response code for failed requests.. How do I do that using the new Spring api? 当然必须能够控制失败请求的http响应代码。如何使用新的Spring api做到这一点?

The doc for setErrorResult method says the following: setErrorResult方法的doc说明如下:

Set an error value for the DeferredResult and handle it. 为DeferredResult设置错误值并处理它。 The value may be an Exception or Throwable in which case it will be processed as if a handler raised the exception. 该值可以是Exception或Throwable,在这种情况下,它将被处理,就像处理程序引发异常一样。

I suppose by setting an Exception , you may trigger an exception handler that returns the code you desire. 我想通过设置Exception ,您可以触发一个返回所需代码的异常处理程序。

deferredResult.setErrorResult(new Exception());

This will always set the HTTP response code to 500. For finer control HttpServletResponse.setStatus seems to work. 这将始终将HTTP响应代码设置为500.为了更好的控制,HttpServletResponse.setStatus似乎可以工作。

This will work with user411180's client side . 这将适用于user411180的客户端

public DeferredResult<List<Point>> getMessages(@RequestParam int reqestedIndex, 
            final HttpServletResponse response) {

    final DeferredResult<List<Point>> deferredResult = new DeferredResult<>();
    deferredResult.onCompletion(...);
    deferredResult.onTimeout(new Runnable() {
        @Override
        public void run() {
            deferredResult.setErrorResult("Explanation goes here.");
            response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); //or SC_NO_CONTENT
        }
    });

    longPollRequests.put(deferredResult, reqestedIndex);
    return deferredResult;
}

The exception that you pass as the argument to setErrorResult can be annotated with @ResponseStatus . 您作为参数传递给setErrorResult的异常可以使用@ResponseStatus进行注释。 eg create an exception class of your own: 例如,创建自己的异常类:

  @ResponseStatus(HttpStatus.NOT_FOUND)
  class NotFoundException extends RuntimeException {
     // add your own constructors to set the error message
     // and/or cause. See RuntimeException for valid ctors 
  }
Then in your code use it with the constructor you have created, for example: 然后在您的代码中使用它与您创建的构造函数,例如:
  deferredResult.setErrorResult(new NotFoundException(reason, cause)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 Spring Boot 错误响应中的状态代码? - How to change Status code in spring boot error response? Spring 带有响应状态代码的 MVC 错误处理 - Spring MVC error handling with response status code 超时时更改deferredResult HTTP状态代码 - Change deferredResult HTTP status code on timeout 如何设置HTTP响应的服务器状态代码? - how to set the server status code for http response? Spring Cloud Gateway:在自定义谓词中设置响应状态代码 - Spring Cloud Gateway: Set response status code in custom predicate Spring 3.2 MVC,如何使用永久状态代码发送重定向视图中的控制器重写URL - Spring 3.2 mvc, how to rewrite url within controller as part of redirectview with permanent status code sent 如何在Spring Boot中处理DeferredResult中的异常? - How to handle exception in DeferredResult in Spring Boot? 如何从 Spring 中提取响应 header 和状态码 5 WebClient ClientResponse - How to extract response header & status code from Spring 5 WebClient ClientResponse Spring Boot和安全性:如何扩展302状态代码的响应? - Spring Boot and security: How to extend response for 302 status code? 如何设置 RestTemplate 以重试某些响应状态代码的调用? - How to set up RestTemplate to retry calls on certain response status code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM