问题
I am using Odata4j for exposing my services and i need to access HttpServletRequest object from my service provider. Is there is a way yo do this. I tried using @Context annotation to inject the object but it is giving null
回答1:
For that you can use servlet filter and get the HTTPRequest object data.
there are two simple step to do that.
1st- define your filter servlet
public class YourRequestFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
if( request.getRequestHeader("userName") != null ) {
String username = request.getRequestHeader("username").get(0);
}
}
}
2nd add this entry into web.xml
<servlet>
<servlet-name>name</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>org.odata4j.jersey.producer.resources.ODataApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.wipro.eim.middleware.framework.oauth.filter.OAuthContainerRequestFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>com.wipro.eim.middleware.framework.oauth.filter.YourRequestFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
来源:https://stackoverflow.com/questions/6675608/how-to-access-httpservletrequest-in-odata-service-provider