org.springframework.web.client.RestClientException: Error while extracting response for type [class com.radar.dto.WorldDTO] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.radar.dto.WorldDTO` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
JSON formate I am trying to fetch:
[
{
"id": "110",
"name": "England",
"areas": [
{
"id": "1620",
"name": "London"
},
...
]
},
...
]
dto classes:
@Data
public class WorldDTO {
private List<CountryDTO> countries;
}
@Data
private static class CountryDTO {
@JsonProperty(value = "id")
private String id;
@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "areas")
private List<AreaDTO> areas;
}
@Data
public class AreaDTO {
@JsonProperty(value = "id")
private String id;
@JsonProperty(value = "name")
private String name;
}
Piece of code where I call API:
RequestEntity<?> request = new RequestEntity<>(HttpMethod.GET, uri);
ResponseEntity<WorldDTO> response = restTemplate.exchange(request, new ParameterizedTypeReference<>() {});
WorldDTO dto = response.getBody();
It seems to me that the error may occur due to the fact that the field private List<CountryDTO> countries; (WorldDTO.class) does not have the @JsonProperty annotation. But there is no name for this object in json either.
What am I doing wrong?