【问题标题】:org.glassfish.jersey.internal.RuntimeDelegateImpl NOT FOUNDorg.glassfish.jersey.internal.RuntimeDelegateImpl 未找到
【发布时间】:2013-10-18 15:03:17
【问题描述】:

我在我的项目中使用 jersey,并试图从字符串中解析 URI。

UriBuilder.fromUri("http://localhost:8000").build();

代码很简单,但下面出现错误

java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl

程序似乎找不到委托。我已经导入了javax.ws.rs.core.UriBuilder 并拥有jersey-common 2.0,它应该在我的构建路径中包含委托。但我仍然收到此错误。

有人知道如何解决吗?谢谢!

【问题讨论】:

    标签: java rest glassfish jersey jax-rs


    【解决方案1】:

    如果您使用的是 Maven,请使用以下依赖项:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.22.2</version>
        <scope>test</scope>
    </dependency>
    

    对于 Gradle,以下内容将起作用:

    testImplementation 'org.glassfish.jersey.core:jersey-common:2.22.2'
    

    【讨论】:

    • 你能解释一下为什么它需要这个依赖
    • 如果您查看 javax.ws.rs.ext.RuntimeDelegate#findDelegate,您会发现它默认使用 org.glassfish.jersey.internal.RuntimeDelegateImpl。在生产环境中,您可能还有其他一些实现,但这不适用于您的测试。
    • 从 cxf 转换到 spring-mvc 时,我删除了一些 cxf 依赖项。按照您的建议添加依赖项可以解决问题。
    • +1 用于 Andrew Swan 的解释 - 这非常有帮助,我现在可以在代码中看到原因。我也是从 cxf 移植的,对为什么突然需要 Jersey 进行单元测试感到困惑。
    • 为什么要指定测试范围?这个类不适合生产吗?
    【解决方案2】:
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.26</version>
        <scope>test</scope>
    </dependency>
    

    我在使用 Java 8 和 jersey-common 2.22.2 时遇到了问题,但 2.26 有效。

    【讨论】:

      【解决方案3】:

      针对 Wildfly 10.1 运行时进行开发我不想将 Jersey 引入我的构建中。我用过 Gradle

      testRuntime "org.jboss.resteasy:resteasy-jaxrs:$versions.resteasy"
      

      resteasy 版本是 3.0.19.Final。这个罐子里有

      META-INF/services/javax.ws.rs.ext.RuntimeDelegate
      

      有一个条目

      org.jboss.resteasy.spi.ResteasyProviderFactory
      

      【讨论】:

        【解决方案4】:

        在我的情况下,问题在于另一个 Jar 被使用,名为: javax.ws.rs-api-2.0.jar

        删除那个 jar 解决了我的问题。

        我用过的罐子:

        <include name="jersey-client-1.9.jar" />
        <include name="jersey-core-1.9.jar" />
        <include name="jersey-multipart-1.9.jar" />
        

        【讨论】:

          【解决方案5】:
          compile 'org.springframework.boot:spring-boot-starter-jersey:1.2.0.RELEASE'
          

          这对我有用!!!

          【讨论】:

            【解决方案6】:

            tl;dr 如果您在一个应用程序中使用 jersey-client 1.x,该应用程序也存在“provider”类,并且那里有 rs-api-2.1.jar,则需要添加 jersey-server 1 .x 或删除 rs-api-2.1 如果你碰巧也有 jsr311-api-1.1.1。

            看来这是正在发生的事情:

            显然 jersey-client 刚出现时,会尝试创建不同 mime 类型的“mime map”。

            jersey-core 1.x ContextResolverFactory:

              public void init(ProviderServices providersServices, InjectableProviderFactory ipf) {
                Map<Type, Map<MediaType, List<ContextResolver>>> rs = new HashMap();
                Set<ContextResolver> providers = providersServices.getProviders(ContextResolver.class);
                Iterator i$ = providers.iterator();
            
                while(i$.hasNext()) {
                  ContextResolver provider = (ContextResolver)i$.next();
                  List<MediaType> ms = MediaTypes.createMediaTypes((Produces)provider.getClass().getAnnotation(Produces.class));
                   ...
            

            因此,如果有任何“提供者”出现(例如:@Produces ({MediaType.WILDCARD})),它会尝试查找他们的类型并将其添加到 mime 类型列表中。但是,它执行以下操作:

            如果你有 rs-api-2.1.jar:

             private static RuntimeDelegate findDelegate() {
               try {
            Object delegate = FactoryFinder.find("javax.ws.rs.ext.RuntimeDelegate", "org.glassfish.jersey.internal.RuntimeDelegateImpl", RuntimeDelegate.class);
            

            如果您有以下其他依赖项,它的实现方式似乎略有不同:

            jsr311-api-1.1.1.jar:

             private static RuntimeDelegate findDelegate() {
               try {
                 Object delegate = FactoryFinder.find("javax.ws.rs.ext.RuntimeDelegate", "com.sun.ws.rs.ext.RuntimeDelegateImpl");
            

            Jersey 1.x 客户端恰好提供了./jersey-client-.19/com/sun/ws/rs/ext/RuntimeDelegateImpl.class

            而 Jersey 1.x 服务器提供:

            ./jersey-server-1.19/com/sun/jersey/server/impl/provider/RuntimeDelegateImpl.class

            jersey 2.0 是“org.glassfish.jersey...”并提供“org.glassfish.jersey.internal.RuntimeDelegateImpl”(显然是理智的)

            所以在我看来rs-api-2.1 基本上默认针对球衣 2.x。但如果您还包含 jersey-server,则恰好可以与 jersey-client 1.x 一起使用。

            使用 jersey 2.x,您不需要以这种奇怪的方式包含“使用客户端的服务器”,似乎可以正常工作。

            我的预感要么是 API 发生了变化(你不应该将 jersey 1.x 与 jsr-2 结合起来吗?嗯?),或者可能是意外的失误、错误或 jersey-client 1.x 的糟糕设计,谁知道呢。

            添加碰巧添加“jersey 2.x”或“jersey-server 1.x”的东西也可以传递。

            这些解决了以下运行时故障:

            Caused by: java.lang.ExceptionInInitializerError: null
                at com.sun.jersey.core.spi.factory.ContextResolverFactory.init(ContextResolverFactory.java:86)
                at com.sun.jersey.api.client.Client.init(Client.java:340)
                at com.sun.jersey.api.client.Client.access$000(Client.java:119)
                at com.sun.jersey.api.client.Client$1.f(Client.java:192)
                at com.sun.jersey.api.client.Client$1.f(Client.java:188)
                at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
                at com.sun.jersey.api.client.Client.<init>(Client.java:188)
                at com.sun.jersey.api.client.Client.<init>(Client.java:171)
                at redacted
                at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
                at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
                at org.familysearch.digitalarchive.aggregator.integration.ServiceAccountConfig$$EnhancerBySpringCGLIB$$a3409578.identityService(<generated>)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:498)
                at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
                ... 29 common frames omitted
            Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
                at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:154)
                at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:121)
                at javax.ws.rs.core.MediaType.valueOf(MediaType.java:196)
                at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
                ... 48 common frames omitted
            Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
                at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
                at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
                at java.lang.Class.forName0(Native Method)
                at java.lang.Class.forName(Class.java:264)
                at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:111)
                at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:209)
                at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:136)
                ... 51 common frames omitted
            

            【讨论】:

              猜你喜欢
              • 2017-11-15
              • 2019-11-09
              • 2020-09-24
              • 2017-01-03
              • 2018-05-03
              • 2014-04-14
              • 1970-01-01
              • 1970-01-01
              • 2022-01-10
              相关资源
              最近更新 更多