无法通过@Context jersey2.x和weblogic 12.1.3在ContainerRequestFilter中注入HttpServletRequest

时间:2015-10-28 19:39:46

标签: jersey-2.0 weblogic12c

我无法使用weblogic 12.1.3通过Jersey 2.22.1中的@Context在ContainerRequestFilter中注入HttpServletRequest。我研究了这个问题存在的几个地方,并且在很多地方我看到它在Jersey 2.4中得到修复,但我仍然看到这个问题。我的实现和代码已附加。如果我遗失任何东西,请告诉我。

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>

我看到的错误在

之下
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)

如果对此问题有任何解决方法,请与我们联系。

2 个答案:

答案 0 :(得分:3)

这似乎是Weblogic 12.1.3的一个错误。我尝试升级到Weblogic 12.2.1并使用#注入1 10001 . 1 10005 NA 1 10019 rs775809821 1 10055 rs768019142 1 10108 rs62651026 ,即使在HttpServletRequest内也能正常工作。

答案 1 :(得分:1)

作为@Context@Inject@Autowire通常首选注入的替代方法,您可以inject HttpServletRequest into a request-scoped bean使用Spring的静态方法:

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

请注意,这种方法并不理想,因为静态方法会使代码更难以测试并使代码更加严格。