开发者

How to access HttpServletRequest in Odata service provider

I am using Odata4j for exposing my services and i need to access HttpServletRequest object from my s开发者_如何转开发ervice provider. Is there is a way yo do this. I tried using @Context annotation to inject the object but it is giving null


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>  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜