How to retrieve JSON Response from a javax.ws.rs.core.Response response?

65,022

Solution 1

You can use following code

String responseAsString = response.readEntity(String.class);

Solution 2

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.

Share:
65,022
pseudoCoder
Author by

pseudoCoder

An IIT graduate. Software Developer at PayPal. Earlier worked in Amazon and HCL.

Updated on February 13, 2022

Comments

  • pseudoCoder
    pseudoCoder over 2 years

    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?