Reputation: 95
I know this is very basic, but I have a problem with deserializing json using jackson when the format is like this :
I created a class Person with id, name and place and tried to read the results from API call using jackson (using the @JsonProperty annotation), but when I debug the persons variable is null:
json body:
{ people:[
{
"id":"0",
"name":"Bob",
"place":"Colorado",
},
{
"id":"1",
"name":"John",
"place":"Chicago",
},
{
"id":"2",
"name":"Marry",
"place":"Miami",
}
]}
RequestEntity<Void> reqEntity = RequestEntity.get(new URI(url))
.accept(MediaType.APPLICATION_JSON)
.build();
ResponseEntity<List<Person>> persons = template.exchange(reqEntity, new ParameterizedTypeReference<List<Person>>() {});
Upvotes: 2
Views: 98
Reputation: 11050
You should wrap your List<Person>
in another Response object, which has a people
field, containing your list:
public class PeopleResponse {
private List<Person> people;
// getter and setter
}
Then you can change your ResponseEntity
according to that:
ResponseEntity<PeopleResponse> response = template.exchange(reqEntity, new ParameterizedTypeReference<PeopleResponse>() {});
List<Person> people = response.getBody().getPeople();
Upvotes: 2