【问题标题】:Cannot inject HttpServletRequest in ContainerRequestFilter via @Context jersey2.x and weblogic 12.1.3无法通过 @Context jersey2.x 和 weblogic 12.1.3 在 ContainerRequestFilter 中注入 HttpServletRequest
【发布时间】:2016-01-28 18:18:52
【问题描述】:

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

授权过滤器

@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)

如果有任何解决此问题的方法,请告诉我。

【问题讨论】:

  • 我也有同样的问题。删除 @Context 解决了这个问题。将请求注入任何其他资源类也可以正常工作!我已经尝试了所有我发现似乎没有任何工作...
  • 您找到问题的解决方案了吗?
  • 没有机会检查您的解决方案。我搬到了 weblogic 12.2.1,它似乎没有这个问题,除了我必须从 weblogic.xml 中删除prefer-application-packages。感谢您的回复,如果我有机会进行测试,我们会通知您。
  • 您没有正确理解。我没有提供解决方案。删除@Context 会停止异常,但我们希望拥有@Context!否则我无法在 ContainerRequestFilter 中获得对 servlet 请求的引用,我需要它。
  • 你是对的。升级到 weblogic 12.2.1 后,此错误不再出现。应该是上个版本的bug。很遗憾,我花了 2 天时间寻找解决方案。

标签: jersey-2.0 weblogic12c


【解决方案1】:

这似乎是 Weblogic 12.1.3 的一个错误。我尝试升级到 Weblogic 12.2.1 并使用 @Context 注入 HttpServletRequest 即使在 ContainerRequestFilter 内也能正常工作。

【讨论】:

    【解决方案2】:

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

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

    请注意,这种方法并不理想,因为静态方法会使您的代码更难测试,并使您的代码更加僵化。

    【讨论】:

    • 我还必须将 &lt;listener&gt;&lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;&lt;/listener&gt; 添加到我的 web.xml 中才能完成这项工作。 RequestContextHolder.currentRequestAttributes() 抛出 IllegalArgumentException 如果它不存在。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 2017-10-21
    • 2016-06-23
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多