Search code examples
web-servicesrestjax-rsresteasyrest-client

Is there a way to read the entity as jax-rs client?


I just made a jax-rs service and I'm trying to convert the String I get from the service to entities. While with jax-rs everything is done automatically on the server side I assume there is a possibility to do it on the client side as well but I'm not finding it.

public class MyClient {

    public static void main(String[] args) {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target("http://localhost:8080/restapp/api/paints/1");
        Response response = target.request().get();
        Paint values = response.readEntity(Paint.class);
        response.close();
    }
}

this give an e:

Exception in thread "main" javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class client.Paint

(It works with String).


Solution

  • You need to add a JSON provider. For RESTeasy, you can see this link and select your version, and add the dependency.

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>${resteasy3.version}</version>
    </dependency>