如何从 javax.ws.rs.core.Response 响应中检索 JSON 响应?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25196427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve JSON Response from a javax.ws.rs.core.Response response?
提问by pseudoCoder
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?
如何json
从 Web 客户端响应中以字符串形式检索响应?
回答by Andrei I
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,则可能必须自己处理输入流。
回答by whoami
You can use following code
您可以使用以下代码
String responseAsString = response.readEntity(String.class);