How to deserialize Json Springboot webservice

Asked

Viewed 806 times

-1

I am trying to consume the themoviedb webservice and am encountering 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": the false, "backdrop_path": "/Auxc0sspzaafddtsmnxsnbeeejr.jpg", "belongs_to_collection": null, "budget": 2000000, "sons-in-law": [ { "id": 18, "name": "Drama" }, { "id": 53, "name": "Thriller" } ], "homepage": null, "id": 55, "imdb_id": "tt0245712", "original_language": "es", "original_title": "Loves 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; an injured Supermodel’s designer Pooch disappears into the Apartment’s floorboards; 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": "es", "name": "Español" } ], "status": "Released", "tagline": "Love. Betrayal. Death." "title": "Loves Perros", "video": false, "vote_average": 7.7, "vote_count": 855 }

There was an Unexpected error (type=Internal Server Error, status=500). Error while extracting [java.util.List] and content type [application/json;charset=utf-8]; nested Exception is org.springframework.http.converter.Httpmessagenotreadableexception: JSON parse error: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token; nested Exception is com.fasterxml.Jackson.databind.exc.Mismatchedinputexception: Cannot 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 Sponse for type [java.util.List] and content type [application/json;charset=utf-8]; nested Exception is org.springframework.http.converter.Httpmessagenotreadableexception: JSON parse error: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token; nested Exception is com.fasterxml.Jackson.databind.exc.Mismatchedinputexception: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: (Pushbackinputstream); line: 1, column: 1]

2 answers

0

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

Edit

Checking your JSON you are getting only one instance of Movie, and is parsing directly to List<Movie>. Change gave code to serialize only Movie instead of the list

  • @Robson if possible also post the return service JSON

  • I ended up doing a mix because I was working on two projects (beginner). Json can be found at https://api.themoviedb.org/3/movie/55?api_key=8a39fa19e513b70d41402ac67813ae35

  • Anyway, you’re only getting a Movie on JSON and deserializing a list.

  • Change the code to consider only Movie, and try again

  • I changed the url to receive a list of movies, and it returns the same error. "https://api.themoviedb.org/3/movie/popular?api_key=8a39fa19e513b70d41402ac67813ae35" It is as if api_key was not passed by restTemplate.

  • I mapped Json with http://www.jsonschema2pojo.org/ to generate the POJO, generating all classes involved, but I was not successful.

Show 1 more comment

0

It would appear that the call to the API 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 Google’s 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: https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.