Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetEntity vs ReadEntity in Response (Javax.ws.rs)

Tags:

java

rest

jax-rs

I am writing a client to consume a RestService and I have to read an entity out of the response and I am totally confused which method out of the two (getEntity vs readEntity) should be used.

I have to retrieve the entity whenever I get a WebApplicationException.

So, my code more or less looks like.

catch(WebApplicationException ex) {
// Do something with ex.getResponse
}

From, whatever I have tested,

ex.getResponse().hasEntity() ---> true

ex.getResponse().getEntity() == null ---> true

I don't know how it is working but if the first is true then how second statement could be true.

Surprisingly, readEntity worked fine for me and I was able to read the entity out from the response.

But, after reading the entity through readEntity,

this call gives false.

 ex.getResponse().getEntity() == null --> false

Can someone help me understand what is really happening behind the scenes?

like image 941
Crosk Cool Avatar asked Feb 14 '18 07:02

Crosk Cool


People also ask

What does Response readEntity do?

readEntity. 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.

What is response return type in Java?

The Response class is an abstract class that contains three simple methods. The getEntity() method returns the Java object you want converted into an HTTP message body. The getStatus() method returns the HTTP response code. The getMetadata() method is a MultivaluedMap of response headers.


2 Answers

The Response class has two uses: server side and client side. On the server side, it's called the outbound response. On the client, it's inbound response.

Outbound

@GET
public Response get() {
    MyModel model = new MyModel();
    return Response.ok(model).build();
}

Inbound

Response response = ClientBuilder.newClient().target(url).request().get();

The getEntity() method is meant to be used on the server side because you want to get the entity object. There's not much use for it for us, but Jersey will use it to get the entity object to serialize it before it sends it out.

The readEntity() method is to be used on the client side because you are trying to read the entity stream. If you try to call this on the server side, you will get an error saying that you can't read the stream on an outbound response.

As far as the behavior you're experiencing, I can't really explain why they implemented like this.

like image 61
Paul Samsotha Avatar answered Sep 30 '22 13:09

Paul Samsotha


This behaviour is documented in the API:

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

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.

[...]

A message instance returned from this method will be cached for subsequent retrievals via getEntity().

The first call to ex.getResponse().getEntity() is null, because the Entity hasn't been read yet. After calling readEntity(), the parsed entity will be returned as parsed by getEntity().

like image 43
Cecilya Avatar answered Sep 30 '22 14:09

Cecilya