使用jersey客户端将JSON响应读取为字符串 [英] reading JSON response as string using jersey client

查看:219
本文介绍了使用jersey客户端将JSON响应读取为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆