How to get list<String> as response from jersey2 client

Saurabh picture Saurabh · Feb 10, 2016 · Viewed 38.8k times · Source

I want to know how I can extract a List<String> as response from the jersey-2.0 client.

I have already tried this,

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

But, the above code is not working. It is not returning the expected List<String> but, a null value instead.

Answer

user2004685 picture user2004685 · Feb 10, 2016

You can get your service response as Response class object and, then parse this object using readEntity(...) method.

Here is a quick code snippet:

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