Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Parse javax.ws.rs.core.Response

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?

like image 326
Geoff Penny Avatar asked Dec 10 '14 03:12

Geoff Penny


People also ask

How do you get entity from response?

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.

How do you return a response 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

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);
like image 156
Paul Samsotha Avatar answered Sep 21 '22 23:09

Paul Samsotha


Also works:

MyResponse myResponse = response.readEntity(MyResponse.class);
like image 20
Justinas Jakavonis Avatar answered Sep 18 '22 23:09

Justinas Jakavonis