Cannot enter HttpServletRequest in ContainerRequestFilter via @Context jersey2.x and weblogic 12.1.3

I was unable to inject the HttpServletRequest into the ContainerRequestFilter via @Context in Jersey 2.22.1 using weblogic 12.1.3. I have explored several places where this problem exists, and in many places I see that it is fixed on Jersey 2.4, but I still see this problem. My implementation and code are attached. Please let me know if I don’t miss anything.

Authfilter

@Provider @Priority(Priorities.AUTHENTICATION) public class AuthFilter implements ContainerRequestFilter { @Context HttpServletRequest webRequest; @Context HttpServletResponse webResponse; @Override public void filter(ContainerRequestContext requestContext) throws IOException { final HttpSession session = webRequest.getSession(); final String userName = (String)session.getAttribute("USER_NAME"); 

web.xml

 <servlet> <servlet-name>jersey-application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value> xyz.xyz.xyz.xyz.xyz.resource</param-value> </init-param> </servlet> <servlet> <servlet-name>authentication-servlet</servlet-name> <servlet-class>xyz.xyz.xyz.xyz.xyz.xyz.AuthenticationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>jersey-application</servlet-name> <url-pattern>/jaxrs/*</url-pattern> </servlet-mapping> 

pom.xml

 <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>2.22.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.ext</groupId> <artifactId>jersey-spring3</artifactId> <version>2.22.1</version> <exclusions> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-beans</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-core</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>spring-web</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>jersey-server</artifactId> <groupId>org.glassfish.jersey.core</groupId> </exclusion> <exclusion> <artifactId> jersey-container-servlet-core </artifactId> <groupId>org.glassfish.jersey.containers</groupId> </exclusion> <exclusion> <artifactId>hk2</artifactId> <groupId>org.glassfish.hk2</groupId> </exclusion> </exclusions> </dependency> 

weblogic.xml

 <?xml version="1.0" encoding="UTF-8"?> <weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"> <library-ref> <library-name>jax-rs</library-name> <specification-version>2.0</specification-version> <implementation-version>2.5.1</implementation-version> </library-ref> <container-descriptor> <prefer-application-packages> <!-- jsr311 --> <package-name>javax.ws.rs.*</package-name> <!-- javassist --> <package-name>javassist.*</package-name> <!-- aop repackaged --> <package-name>org.aopalliance.*</package-name> <!-- jersey 2 --> <package-name>jersey.repackaged.*</package-name> <package-name>org.glassfish.jersey.*</package-name> <package-name>com.sun.research.ws.wadl.*</package-name> <!-- hk2 --> <package-name>org.glassfish.hk2.*</package-name> <package-name>org.jvnet.hk2.*</package-name> <package-name>org.jvnet.tiger_types.*</package-name> </prefer-application-packages> <show-archived-real-path-enabled>true</show-archived-real-path-enabled> <resource-reload-check-secs>0</resource-reload-check-secs> </container-descriptor> <context-root>XYZ</context-root> </weblogic-web-app> 

And the error that I see is below

 Root cause of ServletException. A MultiException has 4 exceptions. They are: 1. java.lang.IllegalArgumentException: interface org.glassfish.hk2.api.ProxyCtl is not visible from class loader 2. java.lang.IllegalArgumentException: While attempting to create a Proxy for javax.servlet.http.HttpServletRequest in scope org.glassfish.jersey.process.internal.RequestScoped an error occured while creating the proxy 3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of xyz.xyz.xyz.xyz.xyz.xyz.xyz.AuthFilter errors were found 4. java.lang.IllegalStateException: Unable to perform operation: resolve on xyz.xyz.xyz.xyz.xyz.xyz.xyz.AuthFilter at org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89) at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:249) at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357) at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471) 

Please let me know in case of any workarounds for this problem.

+8
weblogic12c
source share
2 answers

This seems to be a bug with Weblogic 12.1.3. I tried upgrading to Weblogic 12.2.1 and injecting HttpServletRequest using @Context works correctly even inside ContainerRequestFilter .

+2
source

As an alternative to the usually preferred injection of @Context , @Inject or @Autowire you can introduce the HttpServletRequest into the bean using the static Spring method:

 HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); 

Note that this approach is not ideal because static methods make your code more difficult to test and make your code more rigid.

+1
source

All Articles