使用jersey客户端将JSON响应读取为字符串 [英] reading JSON response as string using jersey client
问题描述
我正在使用jersey客户端将文件发布到REST URI,该URI以json返回响应. 我的要求是按原样(json)读取字符串的响应.
I am using jersey client to post a file to a REST URI that returns response as json. My requirement is to read the response as is(json) to a string.
这是将数据发布到Web服务的一段代码.
Here is the piece of code that posts the data to web service.
final ClientResponse clientResp = resource.type(
MediaType.MULTIPART_FORM_DATA_TYPE).
accept(MediaType.APPLICATION_JSON).
post(ClientResponse.class, inputData);
System.out.println("Response from news Rest Resource : " + clientResp.getEntity(String.class)); // This doesnt work.Displays nothing.
clientResp.getLength()有281个字节,这是响应的大小,但是clientResp.getEntity(String.class)不返回任何内容.
clientResp.getLength() has 281 bytes which is the size of the response, but clientResp.getEntity(String.class) returns nothing.
有什么主意在这里不正确吗?
Any ideas what could be incorrect here?
推荐答案
我能够找到解决问题的方法.只需在getEntity(String.class)之前调用bufferEntity方法.这将以字符串形式返回响应.
I was able to find solution to the problem. Just had to call bufferEntity method before getEntity(String.class). This will return response as string.
clientResp.bufferEntity();
String x = clientResp.getEntity(String.class);
这篇关于使用jersey客户端将JSON响应读取为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!