使用 jersey 客户端将 JSON 响应作为字符串读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19557404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:57:31  来源:igfitidea点击:

reading JSON response as string using jersey client

jsonstringjerseyresponse

提问by Sudhir

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.

我正在使用 jersey 客户端将文件发布到 REST URI,该 URI 将响应作为 json 返回。我的要求是按原样(json)读取字符串的响应。

Here is the piece of code that posts the data to web service.

这是将数据发布到 Web 服务的一段代码。

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() has 281 bytes which is the size of the response, but clientResp.getEntity(String.class) returns nothing.

clientResp.getLength() 有 281 个字节,这是响应的大小,但 clientResp.getEntity(String.class) 什么都不返回。

Any ideas what could be incorrect here?

任何想法在这里可能不正确?

回答by Sudhir

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.

我能够找到问题的解决方案。只需要在 getEntity(String.class) 之前调用 bufferEntity 方法。这将以字符串形式返回响应。

   clientResp.bufferEntity();
   String x = clientResp.getEntity(String.class);

回答by Marcio Jasinski

Although the above answer is correct, using Jersey API v2.7 it is slightly different with Response:

虽然上面的答案是正确的,但使用 Jersey API v2.7 它与Response以下略有不同:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080");
Response response = target.path("api").path("server").path("ping").request(MediaType.TEXT_PLAIN_TYPE).get();
System.out.println("Response: " + response.getStatus() + " - " + response.readEntity(String.class));

回答by dvlcube

In my case I'm using Jersey 1.19 and Genson got in my classpath somehow? So the accepted answer throws com.owlike.genson.stream.JsonStreamException: Readen value can not be converted to String.

就我而言,我使用的是 Jersey 1.19,而 Genson 以某种方式进入了我的类路径?所以接受的答案抛出com.owlike.genson.stream.JsonStreamException: Readen value can not be converted to String.

My solution was to read directly from the response stream:

我的解决方案是直接从响应流中读取:

private String responseString(com.sun.jersey.api.client.ClientResponse response) {
        InputStream stream = response.getEntityInputStream();
        StringBuilder textBuilder = new StringBuilder();
        try (Reader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName(StandardCharsets.UTF_8.name())))) {
            int c = 0;
            while ((c = reader.read()) != -1) {
                textBuilder.append((char) c);
            }
            return textBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

回答by Ian Lim

If you still got problem with this, you may want to consider to use rest-assured

如果您仍然对此有疑问,您可能需要考虑使用rest-assured