I am making a request to an API and getting a response status code of 200
.
Response of the api includes a json
response.
import javax.ws.rs.core.Response; Response response = webclient.post(SomeReqString);
How can I retrieve the json
response as string from the web client 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.
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.
You can use following code
String responseAsString = response.readEntity(String.class);
Try using the Response.getEntity()
method, which returns an InputStream. Then, to convert your InputStream to a String, check this question. If you really need to map the JSON String to a Java entity, that consider calling directly the Response.readEntity()
. Note that, if you consume the InputStream, you will probably have to process the input stream on your own.
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