从post请求中读取JAX-RS客户端中的响应主体 [英] Read response body in JAX-RS client from a post request
本文介绍了从post请求中读取JAX-RS客户端中的响应主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在移动应用和网络服务之间有某种代理,我们对发布帖子请求时的响应感到困惑。我们收到状态为200的回复:好的。但我们无法找到/提取JSON响应主体。
客户端客户端= ClientBuilder.newClient();
WebTarget webTarget = client.target(WEBSERVICE_BASE_LOCATION +mobileDevices?operatorCode = KPNSCP);
字符串jsonString ={\osVersion \:\4.1 \,\apiLevel \:16,\devicePlatform \:\ANDROID \ };
Builder builder = webTarget.request();
响应响应= builder.post(Entity.json(jsonString));
我们正在使用JAX-RS。
有人可以提供一些提示从服务器响应中提取JSON正文( String
)吗?
解决方案
试试这个:
String output = response.getEntity(String.class);
编辑
<感谢@Martin Spamer提到它只适用于Jersey 1.x罐子。对于Jersey 2.x,使用
String output = response.readEntity(String.class);
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?
解决方案
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);
这篇关于从post请求中读取JAX-RS客户端中的响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文