2

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

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.