RESTEasy客户端。找不到内容类型application / xml的编写器

时间:2014-05-27 15:34:56

标签: java eclipse rest maven resteasy

我正在尝试使用RESTeasy连接到Web服务。

我使用的代码是:

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提供商   (版本3.0.8.Final)
  • com.sun.xml.bind

所有这些都在运行时确定范围。

我觉得奇怪的是它在日食下工作。它可能是一些Jaxb配置?或上下文设置。 我也试过

   RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

它没有用。

1 个答案:

答案 0 :(得分:1)

我通过使用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文件。