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. It has a setErrorResult for providing an alternative response if something goes wrong, but no option to supply a http error code.

Surely it must be possible to control the http response code for failed requests.. How do I do that using the new Spring api?

The doc for setErrorResult method says the following:

Set an error value for the DeferredResult and handle it. The value may be an Exception or Throwable in which case it will be processed as if a handler raised the exception.

I suppose by setting an Exception, you may trigger an exception handler that returns the code you desire.

Guide to DeferredResult in Spring, DeferredResult, available from Spring 3.2 onwards, assists in offloading a Let's use the onCompletion() method to define a block of code that's setting a different response status and message body via this error handler. DeferredResult, available from Spring 3.2 onwards, assists in offloading a long-running computation from an http-worker thread to a separate thread. Although the other thread will take some resources for computation, the worker threads are not blocked in the meantime and can handle incoming client requests.

deferredResult.setErrorResult(new Exception());

This will always set the HTTP response code to 500. For finer control HttpServletResponse.setStatus seems to work.

This will work with user411180's client side.

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

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

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

DeferredResult (Spring Framework 5.2.7.RELEASE API), Since: 3.2; Author: Rossen Stoyanchev, Juergen Hoeller, Rob Winch Create a DeferredResult with a timeout value and a default result to use in case of timeout. Register code to invoke when an error occurred during the async request. was set and passed on for handling; false if the result was already set or the async  While it does set the Status Code of the Response properly, one limitation is that it doesn't set anything to the body of the Response. And for a REST API – the Status Code is really not enough information to present to the Client – the response has to have a body as well, to allow the application to give additional information about the

The exception that you pass as the argument to setErrorResult can be annotated with @ResponseStatus. e.g. create an exception class of your own:

  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));

DeferredResult in Spring, Learn how to use DeferredResult class in Spring to create asynchronous endpoints. the asynchronous processing and same was introduced with Spring 3.2. onError – Execute code on error. setErrorResult( ResponseEntity.status(​HttpStatus. new follow-up comments, new replies to my comments. A RESTful application can communicate the success or failure of an HTTP request by returning the right status code in the response to the client. Simply put, an appropriate status code can help the client to identify problems that might have occurred while the application was dealing with the request.

Spring boot async controller with ResponseBodyEmitter , It is a controller method return value type for asynchronous request processing where one or more objects are written to the response. While DeferredResult is  Sometimes a more general status code is easier to implement, as we'll see in our Spring example below. It allows developers to catch groups of exceptions and not differentiate the status code that should be returned. When possible, though, the most specific status code should be used. 4.2. Facebook

Spring Framework 5.1.3.RELEASE Reference, Spring Boot relies on the MVC Java configuration to configure Spring MVC and and maps them to HTTP status codes based on the value in the annotation. up or the response has an error status, the Servlet container makes an ERROR DeferredResult and Callable return values in controller methods and provide  ResponseEntity represents the whole HTTP response: status code, headers, and body.Because of it, we can use it to fully configure the HTTP response. If we want to use it, we have to return it from the endpoint; Spring takes care of the rest.

Index (spring-web 5.0.7.RELEASE API), Write the status code to the underlying response. asMediaType(MimeType) - Static method in class org.springframework.http.MediaType. Re-create the given​  If the exception is thrown, it will create a response with the corresponding response status. (I also took the liberty of changing the status code to 404 - Not Found which I find more appropriate for this use case, but you can stick to HttpStatus.BAD_REQUEST if you like.)