JSON for POJO returning Null

0

I am making a request from the site API swapi and whenever I do the conversion from JSON to POJO, I get the NULL return

RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "https://swapi.co/api/planets/?format=json";
PlanetaEntity response = restTemplate.getForObject(fooResourceUrl, PlanetaEntity.class);

But when there is no conversion, I already direct to String, the return is not null.

RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "https://swapi.co/api/planets/?format=json";
String response = restTemplate.getForObject(fooResourceUrl, String.class);

I believe there is a problem in my PlanetaEntity class, but I have not yet been able to identify it.

@JsonIgnoreProperties(ignoreUnknown = true)

@Document(collection = "starwars")
public class PlanetaEntity implements Serializable {

private static final long serialVersionUID = -8511702512631671990L;

@Id
private ObjectId _id;

private String name;

private String climate;

private String terrain;

private List<String> films
// Getters e Setters

Remembering that I'm using MongoDB for database.

    
asked by anonymous 25.12.2018 / 03:04

1 answer

1

As quoted in the comments, your return JSON has the following structure:

{
"count":61,
"next":"https://swapi.co/api/planets/?format=json&page=2",
"previous":null,
"results":[{..}]
}

Note that the PlanetaEntity class is what is within results . To serialize correctly you need to use the correct matching class. For example:

PlanetaEntityPagination

public class PlanetaEntityPagination {
    private Integer count;
    private String next;
    private String previous;
    private List<PlanetaEntity> results;

    //getters e setters
}

And your code mapping to Jackson deserializes in that class.

    
03.01.2019 / 16:57