在spring boot中将从api接收的嵌套json解析为对象

在spring boot中将从api接收的嵌套json解析为对象,spring,spring-boot,spring-mvc,jackson,Spring,Spring Boot,Spring Mvc,Jackson,我正在创建一个spring引导应用程序,它从第三方api接收一些JSON数据。JSON数据有很多嵌套对象,我想把它们映射成Java对象。下面是我为获取api响应而编写的代码 public ResponseEntity<MovieSearchResultsDto> getMovies(String searchText, String countryCode) { logger.info("GetMovies Service started&

我正在创建一个spring引导应用程序,它从第三方api接收一些JSON数据。JSON数据有很多嵌套对象,我想把它们映射成Java对象。下面是我为获取api响应而编写的代码

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;
        
    }
我所做的是,我创建了一个类MovieSearchResultsTo,并包含一个列表作为其数据成员,其中包含getter和setter

private List<MoviesDto> results = new ArrayList<>();
private List results=new ArrayList();
并创建了MoviesDto类,如下所示

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
}
public class MoviesDto{
私有字符串id;
私有字符串名称;
私有字符串图片;
@JsonInclude(值=Include.NON_EMPTY)
私有列表位置=新的ArrayList();
@JsonInclude(值=Include.NON_EMPTY)
private List external_id=new ArrayList();
公共电影{
}
//接球手和接球手
}
类移动定位{
私有字符串图标;
私有字符串id;
私有字符串显示名称;
私有字符串名称;
私有字符串url;
公共电影定位(){
}
//接球手和接球手
}
类外部项{
私有IdAndUrl imdb;
私人IdAndUrl tmdb;
私有IdAndUrl wiki_数据;
公共外部性(){
}
//接球手和接球手
}
类IdAndUrl{
私有字符串url;
私有字符串id;
公共IdAndUrl(){
}
//接球手和接球手
}
但它在解析时显示错误

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"])
调用API org.springframework.web.client.RestClientException时发生异常:提取类型[class com.prebeesh1427.MovieNameServiceProvider.dto.MovieSearchResultsTo]和内容类型[application/json]的响应时出错;嵌套的异常为org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法反序列化'java.util.ArrayList'的实例,因为它超出了START_对象标记;嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化'java.util.ArrayList'的实例,因为它超出了START_对象标记 在[Source:(PushbackInputStream);第1行,第1054列](通过引用链:com.prebeesh1427.MovieNameServiceProvider.dto.moviesearchresultsdo[“results”]->java.util.ArrayList[0]->com.prebeesh1427.MovieNameServiceProvider.dto.MoviesDto[“external_id”]) 我是这个地区的新手。请不仅帮助我解决这个问题,而且帮助我理解这些解析技术的概念


提前感谢

外部\u ID不在列表中。所以,请删除列表并将其声明为一个普通类,如下所示,private ExternalIds external_ids=new ExternalIds();另外,添加私有字符串提供程序;私人长体重;到MoviesDto.java非常感谢你。我是否遵循了上述编码的良好实践?有没有更好的方法或更标准的方法来实现这一点@你可以用它。见示例:
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"])