![](/img/trans.png)
[英]ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl cannot be found in OSGi bundle which has embedded cxf dependencies
[英]org.glassfish.jersey.internal.RuntimeDelegateImpl NOT FOUND
我在我的项目中使用 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
,它应该在我的构建路径中包含委托。 但我仍然得到这个错误。
有人知道如何解决吗? 谢谢!
如果您使用的是 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'
针对 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
在我的情况下,问题出在使用另一个名为: 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" />
<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 有效。
tl;博士如果您在一个应用程序中使用 jersey-client 1.x,并且该应用程序也存在“提供程序”类,并且您在那里有 rs-api-2.1.jar,则需要添加 jersey-server 1.x 或如果您碰巧也存在 jsr311-api-1.1.1,请删除 rs-api-2.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
默认基本上是针对 jersey 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
请使用 UriComponents 代替 URI
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8000").build();
然后你可以得到URI对象
uriComponents.toUri()
compile 'org.springframework.boot:spring-boot-starter-jersey:1.2.0.RELEASE'
这对我有用!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.