users@jersey.java.net

[Jersey] Access HttpServletRequest and HttpServletResponse in MessageBodyWriter

From: Kevin Zhang <zhangyongjiang_at_yahoo.com>
Date: Fri, 19 Sep 2014 09:48:43 -0700

Hi,





I'm farmiliar with Jersey 1. I'm now switching to Jersey 2 and got some problem with my implementation of MessageBodyWriter.

I used code below to access the HttpServletRequest and HttpServletResponse

    @Context
    private ThreadLocal<HttpServletRequest> requesThreadLocal;

    @Context
    private ThreadLocal<HttpServletResponse> responseThreadLocal;

It worked before with Jersey 1 but now I got NullPointerException.

Specifically, I need to access the HttpServletRequest so that I can call the setAttribute and put beans in the HttpServletRequest object. The beans later will be used by JSP pages. Is there any other alternative?

Your valuable input is hightly appreciated.

Kevin