How to get list as response from jersey2 c

2019-02-16 09:25发布

问题:

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

回答1:

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>>() {
                });


回答2:

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



回答3:

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>>() {
});