How to access HttpServletRequest and HttpServletResponse in REST and SOAP

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 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →