I have a JAX-RS client that's making a simple GET request. I'm using the CXF implementation and Spring for DI. The call is successfull and I get a response code of 200 back. But I'm getting an error when reading the response into my POJO.
Exception:
[2015-05-08 16:11:55,457][ERROR][org.apache.cxf.jaxrs.utils.JAXRSUtils]: No message body reader has been found for class com.voya.refapp.domain.Customer, ContentType: application/json
[2015-05-08 16:11:55,468][ERROR][com.voya.refapp.service.CustomerServiceImpl]: filterByName() - Exception occurred
javax.ws.rs.client.ResponseProcessingException: No message body reader has been found for class com.voya.refapp.domain.Customer, ContentType: application/json
at org.apache.cxf.jaxrs.impl.ResponseImpl.reportMessageHandlerProblem(ResponseImpl.java:433) ~[cxf-rt-frontend-jaxrs-3.0.4.jar:3.0.4]
at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:384) ~[cxf-rt-frontend-jaxrs-3.0.4.jar:3.0.4]
Code:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/rest").path("customers/1");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = builder.get(); // Successful
Customer customer = response.readEntity(Customer.class); // Fails
I have the below dependency as suggested in this answer in my classpath, it doesn't seem to be picked up automatically.
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
I also tried registering the json provider when creating the client:
Client client = ClientBuilder.newClient().register(new JacsksonJsonProvider());
and
Client client = ClientBuilder.newClient().register(JacsksonJsonProvider.class);
But none of these options worked too. I got a different exception when I registered the json provider using one of the options above:
javax.ws.rs.client.ResponseProcessingException: Problem with reading the data
Update:
Registering the json provider worked fine using ClientBuilder.newClient().register(JacsksonJsonProvider.class)
. The issue was with the data (like the exception above clearly states.. I feel silly now :(). I had boolean field in the json named "active", but it was called "isActive" in the POJO. Once I added the annotation @JsonProperty("active")
to the field in POJO, it started working fine
AFAIK CXF does not support autodiscovery of MessageBodyReader
classes. But registering manually JacksonJsonProvider
should work for you.
Please check my example that works perfectly well. It is almost exactly the same as yours, I just used different service. Maybe you can spot a difference that prevents your version from working correctly.
Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class);
WebTarget target = client.target("http://jsonplaceholder.typicode.com").path("posts/1");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON_TYPE);
Response response = builder.get(); // Successful
Post post = response.readEntity(Post.class);