我想知道如何从List<String>
客户端提取jersey-2.0
作为响应。
我已经试过了,
List<String> list = client
.target(url)
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<String>>(){});
但是,上面的代码不起作用。它没有返回预期的List<String>
,而是返回null
值。
您可以将服务响应作为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 */
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
1)在您的Response中使用readEntity()方法解析响应对象。
List<String> list = client.target(url).
request(MediaType.APPLICATION_JSON).get(Response.class).readEntity(new GenericType<List<String>>() {
});