Parsing nested json received from an api to objects in spring boot

Viewed 3131

I am creating a spring boot application which receives some JSON data from a 3rd party api. The JSON data has so many nested objects.I want to map them as Java objects. Below is the code I wrote for getting the api response.

public ResponseEntity<MovieSearchResultsDto> getMovies(String searchText, String countryCode) {
        
        logger.info("GetMovies Service started");
        String url = prepareUrl(searchText,countryCode);
        HttpHeaders header = new HttpHeaders();
        prepareHeader(header);
        HttpEntity<String> requestEntity = new HttpEntity<String>(header);
        try {
            logger.info("Calling the API for movie info");
            responseEntity = restClient.exchange(url,
                    HttpMethod.GET,
                    requestEntity,
                    MovieSearchResultsDto.class);
        }catch (Exception e) {
            logger.error("Exception occured while calling the API "+ e);
            if(responseEntity.getStatusCodeValue() != 200) {

            }
        }
        logger.info("GetMovies Service Ended");
        return responseEntity;
        
    }

And the JSON response looks like

{
    "results": [
        {
            "id": ******,
            "picture": "url",
            "name": "Titanic",
            "locations": [
                {
                    "icon": "url",
                    "display_name": "Amazon Instant Video",
                    "name": "AmazonInstantVideoIVAGB",
                    "id": "***",
                    "url": "url"
                }
            ],
            "provider": "iva",
            "weight": 0,
            "external_ids": {
                "iva_rating": null,
                "imdb": {
                    "url": "url",
                    "id": "tt0046435"
                },
                "tmdb": {
                    "url": "url",
                    "id": "id"
                },
                "wiki_data": {
                    "url": "url",
                    "id": "id"
                },
                "iva": null,
                "gracenote": null,
                "rotten_tomatoes": null,
                "facebook": null
            }
        }
   ]  }

What I have done is , I created a class MovieSearchResultsDto and include a list as its data member with getters and setters.

private List<MoviesDto> results = new ArrayList<>();

And created MoviesDto class as below

public class MoviesDto {

    private String id;
    private String name;
    private String picture;
    @JsonInclude(value = Include.NON_EMPTY)
    private List<MovieLocation> locations = new ArrayList<MovieLocation>();
    @JsonInclude(value = Include.NON_EMPTY)
    private List<ExternalIds> external_ids = new ArrayList<ExternalIds>();
    
    public MoviesDto() {
        
    }
    
    //getters and setters
    
}

class MovieLocation{
    private String icon;
    private String id;
    private String display_name;
    private String name;
    private String url;
    
    public MovieLocation() {
        
    }
    //getters and setters
}

class ExternalIds{
    
    private IdAndUrl imdb;
    private IdAndUrl tmdb;
    private IdAndUrl wiki_data;

    public ExternalIds() {
        
    }
    //getters and setters

    
}

class IdAndUrl{
    private String url;
    private String id;  
    public IdAndUrl() {
        
    }
    //getters and setters
}

But it shows error while parsing.

Exception occured while calling the API org.springframework.web.client.RestClientException: Error while extracting response for type [class com.prebeesh1427.MovieNameServiceProvider.dto.MovieSearchResultsDto] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.prebeesh1427.MovieNameServiceProvider.dto.ExternalIds>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.prebeesh1427.MovieNameServiceProvider.dto.ExternalIds>` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1054] (through reference chain: com.prebeesh1427.MovieNameServiceProvider.dto.MovieSearchResultsDto["results"]->java.util.ArrayList[0]->com.prebeesh1427.MovieNameServiceProvider.dto.MoviesDto["external_ids"])

I am a newbie to this area. Kindly help me not just only to resolve this issue but to understand the concept of these parsing techniques too.

Thanks in advance

0 Answers
Related