Saurabh
Saurabh

Reputation: 7843

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

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.

Upvotes: 36

Views: 56515

Answers (3)

ninja.coder
ninja.coder

Reputation: 9658

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 */

Upvotes: 80

Saurabh
Saurabh

Reputation: 7843

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

Upvotes: 1

Arslan Ahmad
Arslan Ahmad

Reputation: 21

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

Upvotes: -1

Related Questions