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

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.


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.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;
}