How to access HttpServletRequest in Odata service provider

亡梦爱人 提交于 2019-12-06 13:53:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!