我正在尝试使用 RESTeasy 连接到网络服务。
我使用的代码是这样的:
WebTarget resource = client.target(URL_DISPLAY);
Builder request = resource.request(MediaType.APPLICATION_XML);
long startTime = System.currentTimeMillis();
ClientResponse response = (ClientResponse)request.post(Entity.xml(text));
程序在 eclipse 中运行后立即运行。
当我生成可运行的 jar,甚至从控制台运行 java 时,它都不起作用。堆栈跟踪如下:
javax.ws.rs.ProcessingException: Unable to invoke request
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:195)
at webservices.WebServicesTest.requestDisplay(WebServicesTest.java:144)
at webservices.WebServicesTest.main(WebServicesTest.java:328)
Caused by: javax.ws.rs.ProcessingException: could not find writer for content-type application/xml type: webservices.DisplayText
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.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:122)
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)
... 4 more
DisplayText类定义如下:
@XmlRootElement
public class DisplayText implements Serializable
我在我的 pom 中添加了以下包:
- resteasy客户端
- resteasy-jaxrs
- resteasy-jaxb-provider (版本 3.0.8.Final)
- com.sun.xml.bind
所有这些都在运行时范围内。
我觉得奇怪的是它在eclipse下工作。可能是一些 Jaxb 配置?或上下文设置。 我也试过了
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
但它没有用。
最佳答案
我通过使用 maven shade 构建它解决了这个问题。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.google.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这里最重要的一行是:
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
shade插件配置需要the service transformer它合并了 the service discovery mechanism 使用的 META-INF/services 文件.
关于java - RESTEasy 客户端。找不到内容类型应用程序/xml 的编写器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23893501/