具有 Jersey 2.x 和 RESTEasy 依赖项的项目是否可行?

2022-01-21 00:00:00 rest java maven jersey resteasy

我正在开发一个项目,该项目使用各种客户端应用程序作为 maven 依赖项来与不同的 REST API 通信.

I'm working on a project which uses various client apps as maven dependencies to talk to different REST APIs.

但是在包含使用 Jersey 2.x 实现的客户端和使用 RESTEasy 的任何其他客户端时会出现问题.

But there's a problem when including a client which is implemented using Jersey 2.x and any other client which is using RESTEasy.

一旦我添加了使用 RESTEasy 的依赖项,我就会收到如下错误:

As soon as I add the dependency which is using RESTEasy I'll get errors like this:

原因:javax.ws.rs.ProcessingException:无法找到内容类型应用程序/json 和类型类的 MessageBodyReaderde.fhg.ipa.vfk.docker.dockerregistry.restclient.entity.SearchResult在org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:39)在org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73)在org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50)在org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:245)在org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:179)在org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:211)在org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:104)... 4 更多

Caused by: javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json and type class de.fhg.ipa.vfk.docker.dockerregistry.restclient.entity.SearchResult at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:39) at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73) at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50) at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:245) at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:179) at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:211) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:104) ... 4 more

或者这个:

原因:javax.ws.rs.ProcessingException:找不到编写器内容类型应用程序/tar 类型:java.io.FileInputStream atorg.jboss.resteasy.core.interception.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:40)在org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:138)在org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:117)在org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:341)在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.writeRequestBodyToOutputStream(ApacheHttpClient4Engine.java:558)在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.buildEntity(ApacheHttpClient4Engine.java:524)在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:423)在org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:281)... 7 更多

Caused by: javax.ws.rs.ProcessingException: could not find writer for content-type application/tar type: java.io.FileInputStream at org.jboss.resteasy.core.interception.ClientWriterInterceptorContext.throwWriterNotFoundException(ClientWriterInterceptorContext.java:40) at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.getWriter(AbstractWriterInterceptorContext.java:138) at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:117) at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:341) at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.writeRequestBodyToOutputStream(ApacheHttpClient4Engine.java:558) at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.buildEntity(ApacheHttpClient4Engine.java:524) at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:423) at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:281) ... 7 more

是否可以同时使用这两个库或以某种方式配置 maven,以便应用不会尝试使用 RESTEasy 依赖项而不是 Jersey 2.x 依赖项,反之亦然?

Is is possible to use both libraries or configure maven somehow so that the app won't try to use the RESTEasy dependencies instead of the Jersey 2.x ones and vice versa?

谢谢

丹尼尔

推荐答案

您可以尝试使用 Maven 配置文件来激活 Jersey 或 RESTEasy,但您不能在同一个平面类路径中同时拥有两个 JAX-RS 实现时间.

You could try to use Maven profiles to activate either Jersey or RESTEasy, but you can't have two JAX-RS implementations in the same flat classpath at the same time.

如果您必须在一个应用程序中同时使用这两种实现,您将需要由 OSGi 或 JBoss Modules 等模块系统提供的类加载器隔离.

If you have to use both implementations simultaneously in one application, you'll need classloader isolation provided by a module system like OSGi or JBoss Modules.

最好的方法可能是重构您的代码以仅使用 JAX-RS 2.0 客户端 API 并满足于要么 Jersey 或 RESTEasy 用于极端情况需要特定于实现的 API.

The best way to go might be to refactor your code to use only the JAX-RS 2.0 client API and to settle for either Jersey or RESTEasy for corner cases which require implementation-specific APIs.

相关文章