Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTEasy client. could not find writer for content-type application/xml

I am trying to connect to a web-service with RESTeasy.

The code I am using is this:

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));

The program is working as soon as I run it in eclipse.

When I generate a runnable jar, or even run java from the console it does not work. The stack trace is the following:

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

The DisplayText class is defined as follows:

@XmlRootElement
public class DisplayText implements Serializable

I added in my pom the following packages:

  • resteasy-client
  • resteasy-jaxrs
  • resteasy-jaxb-provider (version 3.0.8.Final)
  • com.sun.xml.bind

all of them scoped in runtime.

What I find strange is that it works under eclipse. Is it maybe some Jaxb configuration? or context settings. I also tried

   RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

and it didn't work.

like image 468
Oscar Castiblanco Avatar asked Nov 10 '22 07:11

Oscar Castiblanco


1 Answers

I solved this problem by building it with 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>

The most important line here is this:

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

The shade plugin configuration needs the service transformer which merges the META-INF/services files used by the service discovery mechanism.

like image 147
Jaanus Avatar answered Nov 15 '22 07:11

Jaanus