0
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

1 Answer 1

2

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.