2

I am trying to serialize a jax-rs Response to json string.

The response from the server is json, and I get it from jersey client with:

Response resp = target.request().method("PUT", Entity.json(payloadBean))

where payloadBean is my json request. Everything works fine but I cannot convert the resp in json string in order to log it.

If I try:

String s = EntityUtils.toString((HttpEntity) resp.getEntity());

I get:

org.glassfish.jersey.client.internal.HttpUrlConnector cannot be cast to org.apache.http.HttpEntity

By the way if I don't cast to HttpEntity, compiler says:

toString (org.apache.http.HttpEntity) in EntityUtils cannot be applied to (java.lang.Object).

My relevant imports are:

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

Any ideas?

stelios
  • 2,679
  • 5
  • 31
  • 41

2 Answers2

6

Use resp.readEntity(String.class)

public abstract <T> T readEntity(Class<T> entityType)

Type Parameters: T - entity instance Java type.

Parameters: entityType - the type of entity.

Read the message entity input stream as an instance of specified Java type using a MessageBodyReader that supports mapping the message entity stream onto the requested type.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • If I try this to case (a) I get: "Error reading from entity from input stream", with details message "com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.apache.http.HttpEntity, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1c802d1d; line: 1, column: 1]" – stelios Jan 19 '16 at 10:11
  • Why are you trying to create an HttpEntity. If you want JSON, create a POJO. That's what Jackson is trying to do, convert the JSON into a POJO of type HttpEntity. It can't, so you are getting the exception. If don't want to create a POJO, then just extract it as a String and work with the String. – Paul Samsotha Jan 19 '16 at 10:21
  • ok I got it. The thing is that the response could represent a lot of different POJO classes, thus I was looking for a generic way to parse any structured json response. Anyway thank you for your time – stelios Jan 19 '16 at 10:24
  • 1
    Use `readEntity(new GenericType>(){});`. That will give you a `Map`, but trying to traverse it for random structures may not be so trivial for more complex structures – Paul Samsotha Jan 19 '16 at 10:25
  • Actual answer is below! Thanks! – Mafro34 Nov 11 '16 at 13:08
4

Finally I needed to buffer the message entity data since the stream was consumed and I was getting an error later when I was trying to re-read the response. Thus in order to log it first and then use it again I had to:

resp.bufferEntity();  //need to buffer entity, in order to read the entity multiple times from the Response's InputStream
String s = resp.readEntity(String.class);
stelios
  • 2,679
  • 5
  • 31
  • 41