I am making a request to an API and getting a response status code of 200
.
我正在向API请求并获得200的响应状态代码。
Response of the api includes a json
response.
api的响应包括json响应。
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?
如何从Web客户端响应中将json响应检索为字符串?
2 个解决方案
#1
34
You can use following code
您可以使用以下代码
String responseAsString = response.readEntity(String.class);
#2
6
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.
尝试使用Response.getEntity()方法,该方法返回一个InputStream。然后,要将InputStream转换为String,请检查此问题。如果您确实需要将JSON字符串映射到Java实体,请考虑直接调用Response.readEntity()。请注意,如果您使用InputStream,则可能必须自己处理输入流。
#1
34
You can use following code
您可以使用以下代码
String responseAsString = response.readEntity(String.class);
#2
6
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.
尝试使用Response.getEntity()方法,该方法返回一个InputStream。然后,要将InputStream转换为String,请检查此问题。如果您确实需要将JSON字符串映射到Java实体,请考虑直接调用Response.readEntity()。请注意,如果您使用InputStream,则可能必须自己处理输入流。