Search code examples
restjerseyresponsejersey-client

Jersey REST service not consuming JSON


I have a REST service and client. I am trying to invoke this service to consume a JSON directly and convert it to the object I need. But it's not working. I am getting the following error: A message body reader for Java class com.a.b.c.D, and Java type class com.a.b.c.D, and MIME media type application/json was not found.

Service:

@Path("/getListPrice")
public class ListPriceService {

      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      @Type(PricingObject.class)
      public Response search(PricingObject pricingObject, @Context final HttpHeaders headers) {
              .........
              return Response.ok().entity(pricingObject).build();
      }
}

Client:

WebResource webResource = client.resource(url);               
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                          .type(MediaType.APPLICATION_JSON_TYPE)
                          .post(ClientResponse.class, pricingObjectRequest);

if (response.getStatus() != 200) {
   throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

Can someone tell me what is going wrong?


Solution

  • A message body reader for Java class com.a.b.c.D, and Java type class com.a.b.c.D, and MIME media type application/json was not found

    You didn't say if you are getting a server side exception or a client side exception. If the former, you either don't have a provider for JSON, or you have one but haven't configure it.

    Here is the provider

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey1-version}</version>
    </dependency>
    

    Here is the web.xml configuration

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    

    If it's a client side exception, I would assume you have the above dependency. Then just configure it with the client

    ClientConfig config = new DefaultClientConfig();
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(config);