How to access HttpServletRequest in Odata service

2019-04-16 17:06发布

问题:

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>  


标签: odata odata4j