I’m trying to unmarshall the json array from https://pokeapi.co/api/v2/pokemon/25/encounters, here it’s the resume version:
[
{
"location_area": {
"name": "sinnoh-route-218-area",
"url": "https://pokeapi.co/api/v2/location-area/168/"
}
},
{
"location_area": {
"name": "johto-route-34-area",
"url": "https://pokeapi.co/api/v2/location-area/205/"
}
]
I tried with this:
restTemplate.exchange("https://pokeapi.co/api/v2/pokemon/25/encounters", HttpMethod.GET, null, new ParameterizedTypeReference<LocationAreaEncounters>(){
})
but throws me the next error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type com.bankaya.pokemon.dto.LocationAreaEncounters
from Array value (token JsonToken.START_ARRAY
)
at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION
disabled); line: 1, column: 1]
These are my classes:
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class LocationAreaEncounters {
@JsonProperty("location_area")
private List<Location_Area> locationArea;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonRootName("location_area")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Location_Area {
@JsonProperty("name")
private String name;
@JsonProperty("url")
private String url;
}
Also tried with:
restTemplate.exchange("https://pokeapi.co/api/v2/pokemon/25/encounters", HttpMethod.GET, null, new ParameterizedTypeReference<List<Location_Area>>(){
})
this retrieves the correct number of objects but with null values.
"locationAreas": [
{
"name": null,
"url": null
}
],
What am I doing wrong?
Thank you for your help.
Yerri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.