Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String

I am using RestEasy client with jackson providers and getting the above error

clientside code is:

ClientRequest request = new ClientRequest(url);
request.accept(MediaType.APPLICATION_JSON);
ClientResponse<String> response = request.get(String.class);

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

BufferedReader br =
  new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes())));

response.getEntity() is throwing ClientResponseFailure exception with the error being

Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String

My server side code is below:

@GET
@Path("/{itemId}")
@Produces(MediaType.APPLICATION_JSON)
public String item(@PathParam("itemId") String itemId) {
  //custom code

  return gson.toJSON(object);
}
like image 995
user1632803 Avatar asked Aug 29 '12 10:08

user1632803


4 Answers

You could try to add the following dependency to your maven pom.

   <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <version>2.3.4.Final</version>
   </dependency>
like image 76
Dog Avatar answered Oct 20 '22 06:10

Dog


The problem actually is that RestEasy is unable to find the Jackson provider. I had to manually register it by the following code:

   ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(instance);
    instance.registerProvider(ResteasyJacksonProvider.class);

Everything is working fine with this. But I am still unhappy with the solution as Resteasy is supposed to scan for the providers and register them automatically.

like image 44
user1632803 Avatar answered Oct 20 '22 07:10

user1632803


Client client = ClientBuilder.newBuilder().register(ResteasyJacksonProvider.class).build();
like image 30
Bruno Horta Avatar answered Oct 20 '22 06:10

Bruno Horta


Just adding the line org.jboss.resteasy.plugins.providers.jackson.ResteasyJacksonProvider into META-INF/services/javax.ws.rs.ext.Providers file, solves the problem.

This file is included into resteasy-jackson-providers.jar but same file is also included into another jar, restasy-jaxrs.jar and for an executable jar file, that use both these jars, they are not merged !!

like image 37
MatteoM Avatar answered Oct 20 '22 05:10

MatteoM