Let’s assume we got a Http response
Response response = httpClient.target(url).request().get()
and now we want to read the body of the response as a String
String responseBody = response.readEntity(String.class)
or as an object
User user = response.readEntity(User.class)
Response is closed exception
When we try to read the response body (response.readEntity(..)
) next time we will get an exception java.lang.IllegalStateException: RESTEASY003765: Response is closed
java.lang.IllegalStateException: RESTEASY003765: Response is closed.
at org.jboss.resteasy.specimpl.BuiltResponse.abortIfClosed(BuiltResponse.java:257)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.abortIfClosed(ClientResponse.java:343)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:167)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:218)
...
response.bufferEntity()
All you need to do to prevent that is just to call response.bufferEntity(..)
before reading the response body.
response.bufferEntity();
String responseBody = response.readEntity(String.class);
User user = response.readEntity(User.class);
So now with response.bufferEntity(..)
we can read the response body multiple times.
You may also find these posts interesting: