What I'm doing right now is resulting in a:
java.io.IOException: stream is closed
on the 2nd readEntity() since it closes the stream after the first read.
Here is what I'm doing:
Response response = target.queryParam("start", startIndex)
.queryParam("end", end)
.request()
.accept(MediaType.APPLICATION_XML)
.header(authorizationHeaderName, authorizationHeaderValue)
.get();
String xml = response.readEntity(String.class);
ourLogger.debug(xml);
MyClass message = response.readEntity(MyClass.class); //throws IOException
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.
ok. The ok read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not.
/You can use Response#bufferEntity(), which will allow you to read the entity stream multiple times.
Response response = ...
response.bufferEntity();
String s = response.readEntity(String.class);
MyEntity me = response.readEntity(MyEntity.class);
response.close();
After you read the entity with readEntity(), the result of the reading is cached and is available with the call to getEntity(). This information doesn't really answer the OP's question, but I thought it was useful information to add in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With