How to deserialize Json Springboot webservice

-1

I'm trying to consume the themoviedb webservice and am encountering the following error.

----------------------- POJO ----------------------- -     package com.wsemovie.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Movie {

    public String adult;        
    public String budget;            
    public String  original_language;
    public String  original_title;
    public String  overview;    
    public String  title;


    public Movie() {
        super();
    }
    public String getAdult() {
        return adult;
    }
    public void setAdult(String adult) {
        this.adult = adult;
    }
    public String getBudget() {
        return budget;
    }
    public void setBudget(String budget) {
        this.budget = budget;
    }
    public String getOriginal_language() {
        return original_language;
    }
    public void setOriginal_language(String original_language) {
        this.original_language = original_language;
    }
    public String getOriginal_title() {
        return original_title;
    }
    public void setOriginal_title(String original_title) {
        this.original_title = original_title;
    }
    public String getOverview() {
        return overview;
    }
    public void setOverview(String overview) {
        this.overview = overview;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}

-------------- Controller ---------------------

@Controller
public class MovieController {   
    @GetMapping("/") 
    @ResponseBody
    public String movies(Model model ) {        
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<List<Movie>> resposne = 
                restTemplate.exchange(
                "https://api.themoviedb.org/3/movie/55?api_key=8a39fa19e513b70d41402ac67813ae35",            
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<Movie>>() { }
                );
        List<Movie> lista = resposne.getBody();

        model.addAttribute("lista", lista);
        return "movie"; //Invocando o template thymeleaf
    }   
}

---------------------- JSON ------------------------ -----------

{     "adult": false,     "backdrop_path": "/AuXC0SsPZaAfddtSMnxSnbEeEjR.jpg",     "belongs_to_collection": null,     "budget": 2000000,     "genres": [         {             "id": 18,             "name": "Drama"         },         {             "id": 53,             "name": "Thriller"         }     ],     "homepage": null,     "id": 55,     "imdb_id": "tt0245712",     "original_language": "is",     "original_title": "Amores perros",     "overview": "Three different people in Mexico City are catapulted into dramatic and unforeseen circumstances in the wake of a terrible car crash: a young punk stumbles into the sinister underground world of dog fighting; ; and an ex-radical turned hit man rescues a gunshot Rotweiler. ",     "popularity": 5,686,     "poster_path": "/8gEXmIzw1tDnBfOaCFPimkNIkmm.jpg",     "production_companies": [         {             "id": 5084,             "logo_path": null,             "name": "Altavista Films",             "origin_country": ""         },         {             "id": 11230,             "logo_path": null,             "name": "Zeta Film",             "origin_country": ""         }     ],     "production_countries": [         {             "iso_3166_1": "MX",             "name": "Mexico"         }     ],     "release_date": "2000-06-16",     "revenue": 20908467,     "runtime": 154,     "spoken_languages": [         {             "iso_639_1": "is",             "name": "Spanish"         }     ],     "status": "Released",     "tagline": "Love. Betrayal. Death.",     "title": "Amores Perros",     "video": false,     "vote_average": 7.7,     "vote_count": 855 }

  

There was an unexpected error (type = Internal Server Error,   status = 500). Error while extracting response for type   [java.util.List] and content type   [application / json; charset = utf-8]; nested exception is   org.springframework.http.converter.HttpMessageNotReadableException:   JSON parse error: Can not deserialize instance of java.util.ArrayList   out of START_OBJECT token; nested exception is   com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not   deserialize instance of java.util.ArrayList out of START_OBJECT   token at [Source: (PushbackInputStream); line: 1, column: 1]   org.springframework.web.client.RestClientException: Error while   extracting response for type   [java.util.List] and content type   [application / json; charset = utf-8]; nested exception is   org.springframework.http.converter.HttpMessageNotReadableException:   JSON parse error: Can not deserialize instance of java.util.ArrayList   out of START_OBJECT token; nested exception is   com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not   deserialize instance of java.util.ArrayList out of START_OBJECT   token at [Source: (PushbackInputStream); line: 1, column: 1]

    
asked by anonymous 20.12.2018 / 01:04

2 answers

0

By all indications, the API call would return some attributes that would make up your Movie object, not a list of them. You could try the following approach: deserialize JSON by populating a Movie object and then adding the object to the list with the Google gson library:

Dependency:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.6.2</version>
</dependency>

Implementation:

Gson gson = new Gson(); //instancie o obj Gson
String jsonInString = resposne.getBody(); //se não for uma String, o que acho difícil, converta
Movie filme = new Movie();
filme = gson.fromJson(jsonInString, Movie.class);
lista.add(filme);

For more details, take a look:

    
20.12.2018 / 13:05
0

From what I saw you are deserializing a List<Cotacao> and not a Movie as you included in the question, probably the JSON return is not a list and you are trying to deserialize to list.

Edit

By checking your JSON you are only receiving one instance of Movie , and you are parsing directly to List<Movie> . Swap code to only serialize Movie instead of list

    
20.12.2018 / 09:52