Home > OS >  I'm recieving "Error while extracting response for type" when I try to return the JSO
I'm recieving "Error while extracting response for type" when I try to return the JSO

Time:09-15

I'm doing an API that consumes an external API (https://swapi.dev/), but I'm receving this error when I try to return the JSON converted to Object.

I'm trying to solve this for almost 12 hours without any success x_x

Error while extracting response for type [class [Lcom.starwarsapi.filmsapi.model.FilmModel;] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `[Lcom.starwarsapi.filmsapi.model.FilmModel;` from Object value (token `JsonToken.START_OBJECT`);

FilmController:

@RestController
@RequestMapping("/films")
public class FilmController {

    @Autowired
    private FilmService filmService;

    @GetMapping
    public List<FilmModel> getAllFilms() {
        List<FilmModel> response = filmService.getAllFilms();
        return response;
    }

FilmModel:

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class FilmModel {
    private FilmResultModel[] results;
}

FilmResultModel

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class FilmResultModel {
    private String title;

    @JsonProperty("episode_id")
    private Integer episodeId;

    @JsonProperty("opening_crawl")
    private String description;

    private String director;
    private String producer;

    @JsonProperty("release_date")
    private String releaseData;

    private String[] characters;
    private String[] planets;
    private String[] starships;
    private String[] vehicles;
    private String[] species;
    private String created;
    private String edited;
    private String url;

FilmService:

public interface FilmService {
    public List<FilmModel> getAllFilms();
}

FilmServiceImpl:

@Service
public class FilmServiceImpl implements FilmService {

    @Value("${external.api.url}")
    private String filmBaseUrl;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<FilmModel> getAllFilms() {
        FilmModel[] result = restTemplate.getForObject(filmBaseUrl, FilmModel[].class);
        List<FilmModel> films = Arrays.asList(result);
        System.out.println(films);
        return films;
    }

PS¹: external.api.url = https://swapi.dev/api/films/?format=json

PS²: When I return getAllFilms as String, the program works:

@Override
public String getAllFilms() {
    String result = restTemplate.getForObject(filmBaseUrl, String.class);
    System.out.println(result);

    return result;
}

But I need it to return as an object because later I'll try to create a PUT method to change the description of the movie.

CodePudding user response:

try using FilmModel result = restTemplate.getForObject(filmBaseUrl, FilmModel.class);

FilmModel is just the outer wrapper object and contains the list of films.

  • Related