Read response body in JAX-RS client from a post request
Solution 1
Try this:
String output = response.getEntity(String.class);
EDIT
Thanks to @Martin Spamer to mention that it will work for Jersey 1.x jars only. For Jersey 2.x use
String output = response.readEntity(String.class);
Solution 2
I just found a solution for jaxrs-ri-2.16 - simply use
String output = response.readEntity(String.class)
this delivers the content as expected.
Solution 3
For my use case, none of the previous answers worked because I was writing a server-side unit test which was failing due the following error message as described in the Unable to Mock Glassfish Jersey Client Response Object question:
java.lang.IllegalStateException: Method not supported on an outbound message.
at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:145)
at ...
This exception occurred on the following line of code:
String actJsonBody = actResponse.readEntity(String.class);
The fix was to turn the problem line of code into:
String actJsonBody = (String) actResponse.getEntity();
Solution 4
I also had the same issue, trying to run a unit test calling code that uses readEntity
. Can't use getEntity
in production code because that just returns a ByteInputStream
and not the content of the body and there is no way I am adding production code that is hit only in unit tests.
My solution was to create a response and then use a Mockito spy to mock out the readEntity
method:
Response error = Response.serverError().build();
Response mockResponse = spy(error);
doReturn("{jsonbody}").when(mockResponse).readEntity(String.class);
Note that you can't use the when(mockResponse.readEntity(String.class)
option because that throws the same IllegalStateException
.
Hope this helps!
user2657714
Updated on May 13, 2020Comments
-
user2657714 about 4 years
Having some sort of proxy between a mobile app and a web-service, we are puzzled by the response when issuing a post request. We receive response with status 200: OK. But we can not find/extract the JSON response body.
Client client = ClientBuilder.newClient(); WebTarget webTarget = client.target(WEBSERVICE_BASE_LOCATION + "mobileDevices?operatorCode=KPNSCP"); String jsonString = "{\"osVersion\":\"4.1\",\"apiLevel\":16,\"devicePlatform\":\"ANDROID\"}"; Builder builder = webTarget.request(); Response response = builder.post(Entity.json(jsonString));
We are using JAX-RS. Can someone please provide some hints to extract the JSON body (
String
) from the server response? -
Martin Spamer almost 10 yearsThis will only work with Jax 1.x, from Jersey: 2.x onward which the OP is using (We can tell from his use of ClientBuilder.newClient();). The response is
response.readEntity(String.class);
-
col.panic about 9 yearsThis does not work for jaxrs-ri-2.16, here you get
org.glassfish.jersey.client.HttpUrlConnector$2@1255b1d1
as output. -
Alfishe about 9 yearsJersey developers drive me crazy. Every version methods naming and/or the whole workflow changes drastically. It's like playing Miner game. Find all mines from scratch.
-
Whimusical about 8 yearsDon't you like Agility? :)
-
spectre007 almost 8 yearsthanks, I got stuck into this for long time as I was using getEntity() to read response message body. getEntity() works only with Jersey 1.x and Jersey 2.0 has support for response.readEntity() to get response message body.
-
Ben almost 8 yearsgetEntity won't automatically read any backing InputStream, so be careful.
-
Hristo Vrigazov about 6 years@Whimusical I think he likes backward compatibility :)
-
John Calcote about 5 yearsThank you! You're probably the first person to actually understand the OP's question. For all you other first-responders out there: no one cares how smart you are. They want to fix their problem. They're not interested in why jersey doesn't let you call readResponse in a unit test.
-
dvlcube about 4 yearsNote that if you get a
com.owlike.genson.stream.JsonStreamException: Readen value can not be converted to String
, you'll need to read the response stream manually to get the JSON String. That happened for me with Jersey 1.19.