How to get list as response from jersey2 c

2019-02-16 09:37发布

I have tried this ,

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

but I am not getting the list instead I am getting null value

3条回答
在下西门庆
2楼-- · 2019-02-16 09:50

Take your Response in the Response Object and then parse the Response Object using readEntity() method.

Here is a quick code snippet:

Response serviceResponse = client.target(url).
                    request(MediaType.APPLICATION_JSON).get(Response.class);
List<String> list = serviceResponse.readEntity(new GenericType<List<String>>() {
                });
查看更多
Bombasti
3楼-- · 2019-02-16 09:50

1) Take your Response in the then parse the Response Object using readEntity() method.

List<String> list = client.target(url).
request(MediaType.APPLICATION_JSON).get(Response.class).readEntity(new GenericType<List<String>>() {
});
查看更多
甜甜的少女心
4楼-- · 2019-02-16 10:03
String listString= serviceResponse.readEntity(String.class);
Gson gson=new Gson();
Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = gson.fromJson(listString, type);

Get response string and then convert to List by using gson library

查看更多
登录 后发表回答