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?
 OneCricketeer
 
 193k20 gold badges145 silver badges276 bronze badges
 
 
 asked Oct 30, 2020 at 23:52
 
 
 
 NeverSleeps 
 
 2,0102 gold badges28 silver badges50 bronze badges
 
 1 Answer 1
You don't need WorldDTO class.
It thinks you're trying to parse an object
{ "countries" : [ {"id": "...", "name": ".."... ]}
You should be using a ResponseEntity<List<CountryDTO>>
 answered Oct 30, 2020 at 23:56
 
 
 
 OneCricketeer 
 
 193k20 gold badges145 silver badges276 bronze badges
 
 
 Sign up to request clarification or add additional context in comments.
 
 
 
 Comments
lang-java