我使用Odata4j来公开我的服务,我需要从我的服务提供商访问HttpServletRequest对象。有没有办法哟这样做。我尝试使用@Context注释来注入对象,但它给出了null
答案 0 :(得分:0)
为此,您可以使用servlet过滤器并获取HTTPRequest对象数据。
有两个简单的步骤可以做到。
1st-定义过滤器servlet
public class YourRequestFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
if( request.getRequestHeader("userName") != null ) {
String username = request.getRequestHeader("username").get(0);
}
}
}
第二次将此条目添加到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>