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);
}
I had a similar problem and I realized that the problem was related with the version of
resteasy-jackson-provider
. I just moved from3.0.4.Final
to3.0.5.Final
and the problem disappeared.Additionally I also realized that if I change the third line to the following the result was the expected with no need to change the dependencies.
You could try to add the following dependency to your maven pom.
Things that had made work my code were that I added:
Beside this I don't know why but it seems that resteasy not initializing providers when client were created, this means that it required to init them manually:
In general it's enough to run the client.
If you really want to by-pass goodness of JAX-RS actually doing serialization for you (instead of manually calling GSON), use
StreamingOutput
(i.e. wrap outputter asStreamingOutput
, return that object).I don't know the full rationale behind that but we've hit the exact same problem (multiple times :P) and you need to change the MediaType to
TEXT_PLAIN
.Or you can also let JAX-RS do the job for you: instead of doing gson.toJSON(object), simply return object and change your method signature to whatever class that is. JAX-RS (RESTEasy in your case) will automatically call Jackson (if it's properly configured) and serialize your object to json. Then on your client side you would request for that same class instead of String and everything should work on its own. I'm not particularly familiar with ClientRequest/Response so it might not work as I said; we use RESTEasy proxy functionality on the client side instead (see
ProxyFactory
). Nevertheless, JAX-RS/RESTEasy can do json serialize/deserialize automatically on the client side too so there's definetly a way.