Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javax.ws.rs.core.Response.readEntity to extract a list of strings from a JSON object

Tags:

json

jax-rs

If my response is like:

{
  "values": [ "1", "2" ]
}

How should I use readEntity to populate a List<String> with the values: 1, 2?

like image 260
Baz Avatar asked Oct 22 '25 13:10

Baz


1 Answers

You can read the entity as a Map<String, List<String>>:

Map<String, List<String>> map = 
    response.readEntity(new GenericType<Map<String, List<String>>>() { });
List<String> values = map.get("values");

Or define your own POJO:

public class MyBean {

    private List<String> values;

    // Getters and setters
}
List<String> values = response.readEntity(MyBean.class).getValues();

You obviously must have a JSON provider such as Jackson registered.

like image 199
cassiomolin Avatar answered Oct 25 '25 21:10

cassiomolin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!