如何获得清单 作为来自jersey2客户的回复

问题描述 投票:16回答:3

我想知道如何从List<String>客户端提取jersey-2.0作为响应。

我已经试过了,

List<String> list = client
                      .target(url)
                      .request(MediaType.APPLICATION_JSON)
                      .get(new GenericType<List<String>>(){});

但是,上面的代码不起作用。它没有返回预期的List<String>,而是返回null值。

java json jersey-2.0 jersey-client
3个回答
44
投票

您可以将服务响应作为Response类对象获取,然后使用readEntity(...)方法解析此对象。

这是一个快速的代码片段:

List<String> list = client
                      .target(url)
                      .request(MediaType.APPLICATION_JSON)
                      .get(Response.class)
                      .readEntity(new GenericType<List<String>>() {});
/* Do something with the list object */

0
投票
String listString= serviceResponse.readEntity(String.class);
Gson gson=new Gson();
Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = gson.fromJson(listString, type);

获取响应字符串,然后使用gson库转换为List


-2
投票

1)在您的Response中使用readEntity()方法解析响应对象。

List<String> list = client.target(url).
request(MediaType.APPLICATION_JSON).get(Response.class).readEntity(new GenericType<List<String>>() {
});
© www.soinside.com 2019 - 2024. All rights reserved.