I am having trouble understanding how to parse javax.ws.rs.core.Response. Some people have pointed to using an InputStream, but I am not understanding how that works since the return type of response.getEntity() is of type Object. For example:
Response response = client.target(enpoint).request(MediaType.APPLICATION_XML).get();
InputStream is = response.getEntity();
NetBeans complains and says I will need to cast type Object to InputStream. The response is going to consist of XML and I just want to be able to parse it with DOM. I am having trouble getting from javax.ws.rs.core.Response to anything useful.
Any ideas?
1 Answer. Show activity on this post. You can use: 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.
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.
For JAX-RS 2.x Client API, use Response.readEntity(InputStream.class)
. Alternatively, is you don't need any specific information from the Response
object, you can simple do
InputStream is = client.target(enpoint).request(
MediaType.APPLICATION_XML).get(InputStream.class);
Also works:
MyResponse myResponse = response.readEntity(MyResponse.class);
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