How to access HttpServletRequest and HttpServletResponse in both REST and SOAP service

There are times when we need either HttpServletRequest or HttpServletResponse in our REST or SOAP api. These objects are readily available if you know how to inject them.

Common usage of HttpServletRequest is when getting the request variables or for HttpServletResponse when downloading a file.

How to inject:
REST:

In REST it's as simply as injecting the HttpServlet object. For example:

@Context 
private HttpServletResponse httpServletResponse;

@Context
private HttpServletRequest httpServletRequest;


As for SOAP, it's almost the same:

@Resource
protected WebServiceContext webServiceContext;

MessageContext mc = webServiceContext.getMessageContext();
HttpServletResponse response = (HttpServletResponse) mc.get(MessageContext.SERVLET_RESPONSE);

0 nhận xét:

Đăng nhận xét