How to register jackson json provider for cxf jax-rs 2.0 client?

I have a JAX-RS client that makes a simple GET request. I am using the CXF and Spring implementation for DI. The call completed successfully and I get a 200 response code back. But I get an error while reading the response in my POJO.

An 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] 

The 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 dependency below suggested in this answer in my classpath, it doesn't seem to be matched 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. I got another 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 a json provider worked fine using ClientBuilder.newClient().register(JacsksonJsonProvider.class) . The problem was that the data (for example, the above exception is clearly stated). I feel stupid now :() I had a logical field in json called "active", but it was called "isActive" in POJO. @JsonProperty("active") annotation in a field in POJO, it started working fine

+7
java rest jackson jax-rs cxf
source share
1 answer

AFAIK CXF does not support auto-detection of MessageBodyReader classes. But manually registering JacksonJsonProvider should work for you.

Please check out my example , which works just fine. This is almost the same as yours, I just used different services. You may find a difference that prevents your version from working properly.

 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); 
+8
source share

All Articles