7

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?

Geoff Penny
  • 71
  • 1
  • 1
  • 4
  • This link may help you http://stackoverflow.com/questions/18086621/read-response-body-in-jax-rs-client-from-a-post-request – Jamsheer Dec 10 '14 at 04:30

3 Answers3

8

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);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Ah. That's what I was trying to accomplish (.readEntity(InputStream.class) and it makes perfect sense. Thank you. – Geoff Penny Dec 10 '14 at 22:37
  • It actually doesn't make perfect sense. It is a terrible API design to overload a method that would normal convert the content to the given type with one that returns the input stream for the content instead. But... thank you for this, it is hopefully what I need! – Alex Worden Dec 30 '20 at 01:25
3

Also works:

MyResponse myResponse = response.readEntity(MyResponse.class);
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
-1

InputStream responseBody = (InputStream) response.getEntity();