Skip to content
Advertisement

Spring DeferredResult setResult causing 404

I am using the new spring 3.2 async servlet requests. All works well except in this one scenario…

@RequestMapping("/test")
@ResponseBody
public DeferredResult<byte[]> test(InputStream is, HttpServletResponse response, WebRequest request) {
    if (someTest()) {
        deferredResult.setResult(DATA);
        return deferredResult;
    }

    deferredResult.onTimeout(new Runnable() {

        @Override
        public void run() {
            deferredResult.setResult(DATA);
        }
    });

    // Use deferredResult in another thread when needed

    return deferredResult;
}

When someTest() is true spring/tomcat returns a 404. I am not sure how I should go about this or why this happens? I assume it might be because the Servlet Async Context has already been created. Is this a bug or am I doing something wrong?

Any info will be appreciated!

Advertisement

Answer

I figured it out, see below…

@RequestMapping("/test")
@ResponseBody
public DeferredResult<byte[]> test(InputStream is, HttpServletResponse response, WebRequest request) {
    if (someTest()) {
        response.getOutputStream().write(DATA);
        return null;
    }

    deferredResult.onTimeout(new Runnable() {

        @Override
        public void run() {
            deferredResult.setResult(DATA);
        }
    });

    // Use deferredResult in another thread when needed

    return deferredResult;
}

Trick is to return null and write the response manually to the ServletResponse.

Hope this comes in use to someone else 🙂

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement