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

Osc*_*nco 5 java eclipse rest resteasy maven

我正在尝试使用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));
Run Code Online (Sandbox Code Playgroud)

我在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
Run Code Online (Sandbox Code Playgroud)

DisplayText类定义如下:

@XmlRootElement
public class DisplayText implements Serializable
Run Code Online (Sandbox Code Playgroud)

我在我的pom中添加了以下包:

  • RestEasy的客户端
  • RestEasy的-JAXRS
  • resteasy-jaxb-provider(版本3.0.8.Final)
  • com.sun.xml.bind

所有这些都在运行时范围内.

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

   RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
Run Code Online (Sandbox Code Playgroud)

它不起作用.

Jaa*_*nus 1

我通过使用 Maven 阴影构建它解决了这个问题。

<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>
Run Code Online (Sandbox Code Playgroud)

这里最重要的一行是:

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
Run Code Online (Sandbox Code Playgroud)

阴影插件配置需要服务转换器,它合并服务发现机制使用的 META-INF/services 文件。