注入上下文HttpServletRequest原因"接口org.glassfish.hk2.api.ProxyCtl在类加载器中不可见" (球衣2.21,weblogic 12.1.3)

时间:2015-08-27 15:50:27

标签: java jersey-2.0 weblogic12c

我在weblogic 12c(12.1.3)上使用jersey 2.21来提供Rest。 Rest类工作正常,无需注入上下文HTTPServeletRequest / HTTPServletResponse。但是一旦注入上下文,就会抛出"接口org.glassfish.hk2.api.ProxyCtl在类加载器中是不可见的#34;错误,在调用服务时。

这是班级:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Path("test")
public class TestResource {
Logger logger = LoggerFactory.getLogger(TestResource.class);


@Context HttpServletResponse resp; 
@Context HttpServletRequest request; 


/**
 * Test site connectivity. Return parameter txt's content in reply
 * @param content
 * @return 
 */
@GET
@Path("/tryMe")
@Produces(MediaType.TEXT_PLAIN)
public String echoTest(@QueryParam("txt") String content) { 
    System.out.println("Try me reach.");
    System.out.println("from client: "+request.getRemoteAddr()); 


    return content;
}

}

如果注释掉这两个上下文,则在调用tryMe Restful时没有错误 当使用两个上下文时,调用tryMe restful会抛出以下错误:

Root cause of ServletException.
        A MultiException has 2 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

            at org.jvnet.hk2.internal.ProxyUtilities.generateProxy(ProxyUtilities.java:225)
            at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2044)
            at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
            at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
            at org.glassfish.jersey.internal.inject.ContextInjectionResolver.resolve(ContextInjectionResolver.java:126)
            Truncated. see log file for complete stacktrace
        Caused By: java.lang.IllegalArgumentException: interface org.glassfish.hk2.api.ProxyCtl is not visible from class loader
            at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:581)
            at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:557)
            at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230)
            at java.lang.reflect.WeakCache.get(WeakCache.java:127)
            at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419)
            Truncated. see log file for complete stacktrace

这是weblogic.xml

    <?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.7/weblogic-web-app.xsd">
<wls:weblogic-version>12.1.3</wls:weblogic-version>
<wls:context-root>demoRest</wls:context-root>
<wls:container-descriptor>
    <wls:prefer-web-inf-classes>false</wls:prefer-web-inf-classes>
    <wls:prefer-application-packages>
        <!-- slf4j -->
        <wls:package-name>org.slf4j</wls:package-name>

        <!-- jersey-bundle-*.jar -->
        <wls:package-name>com.sun.jersey.*</wls:package-name>
        <wls:package-name>com.sun.research.ws.wadl.*</wls:package-name>
        <wls:package-name>com.sun.ws.rs.ext.*</wls:package-name>

        <!-- Jackson-*.jar -->
        <wls:package-name>org.codehaus.jackson.*</wls:package-name>

        <!-- jsr311*.jar -->
        <wls:package-name>javax.ws.rs.*</wls:package-name>

        <!-- javassist -->
        <wls:package-name>javassist.*</wls:package-name>
        <!-- aop repackaged -->
        <wls:package-name>org.aopalliance.*</wls:package-name>


        <wls:package-name>com.sun.jersey.*</wls:package-name>
        <wls:package-name>org.glassfish.jersey.*</wls:package-name>
        <wls:package-name>jersey.repackaged.org.objectweb.asm.*</wls:package-name>
        <wls:package-name>org.objectweb.asm.*</wls:package-name>
        <wls:package-name>com.sun.ws.rs.ext.*</wls:package-name>
        <wls:package-name>javax.ws.rs.*</wls:package-name>
        <wls:package-name>com.sun.research.ws.wadl.*</wls:package-name>
        <wls:package-name>jersey.repackaged.*</wls:package-name>

        <!-- hk2 -->
        <wls:package-name>org.glassfish.hk2.*</wls:package-name>
        <wls:package-name>org.jvnet.hk2.internal.*</wls:package-name>
        <wls:package-name>org.jvnet.hk2.*</wls:package-name>
        <wls:package-name>org.jvnet.tiger_types.*</wls:package-name>


        <!-- jpa2.1 hibernate -->
        <wls:package-name>javax.persistence.*</wls:package-name>
        <wls:package-name>org.hibernate.*</wls:package-name>
        <wls:package-name>org.hibernate.validator.*</wls:package-name>
        <wls:package-name>javax.validation.*</wls:package-name>

        <!-- commons io <wls:package-name>org.apache.commons.io.*</wls:package-name> -->
    </wls:prefer-application-packages>


    <wls:prefer-application-resources>
        <wls:resource-name>META-INF/services/javax.servlet.ServletContainerInitializer</wls:resource-name>
        <wls:resource-name>META-INF/services/javax.ws.rs.ext.RuntimeDelegate</wls:resource-name>

        <!-- jersey -->
        <wls:resource-name>META-INF/services/org.glassfish.jersey.*</wls:resource-name>
        <wls:resource-name>org.glassfish.jersey.*</wls:resource-name>
        <wls:resource-name>jersey.repackaged.*</wls:resource-name>

        <!-- hk2 -->
        <wls:resource-name>META-INF/services/org.glassfish.hk2.*</wls:resource-name>

        <!-- slf4j -->
        <wls:resource-name>org.slf4j</wls:resource-name>

        <!-- jersey-bundle-*.jar -->
        <wls:resource-name>com.sun.jersey.*</wls:resource-name>
        <wls:resource-name>com.sun.research.ws.wadl.*</wls:resource-name>
        <wls:resource-name>com.sun.ws.rs.ext.*</wls:resource-name>

        <!-- Jackson-*.jar -->
        <wls:resource-name>org.codehaus.jackson.*</wls:resource-name>

        <!-- jsr311*.jar -->
        <wls:resource-name>javax.ws.rs.*</wls:resource-name>

        <!-- jpa2.1 hibernate -->
        <wls:resource-name>javax.persistence.*</wls:resource-name>
        <wls:resource-name>org.hibernate.*</wls:resource-name>
        <wls:resource-name>org.hibernate.validator.*</wls:resource-name>
        <wls:resource-name>javax.validation.*</wls:resource-name>

        <!-- commons io -->
        <wls:resource-name>org.apache.commons.io.*</wls:resource-name>
    </wls:prefer-application-resources>
</wls:container-descriptor>

4 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并找到了解决方法:我将请求注入方法声明并为我工作:

    @GET
    public void test(@Context HttpServletRequest request){
        ...
    }

答案 1 :(得分:0)

我发现将它作为Component似乎存在一些问题,所以我不得不删除注释@Component。然后错误就消失了。我看到glassfish中提到了一些问题:Glassfish issue

但我不确定这些是否已经修复或与泽西岛有关。如果你没有@Component,那么这可能是最好的解决方案。

答案 2 :(得分:0)

检查duplicate topic,如果你必须使用静态访问

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

答案 3 :(得分:0)

在weblogic 12c中部署rest项目时我也遇到了同样的问题,jediz建议尝试使用该解决方案。此外,您必须在web.xml中添加侦听器。

 <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>