RestTemplate在序列化为POJO时返回null

时间:2017-01-31 09:27:32

标签: java spring-boot resttemplate

我正在关注春季网站https://spring.io/guides/gs/consuming-rest/的启动指南。

在我使用另一个端点的意义上,我没有遵循确切的教程:http://www.omdbapi.com?s=rush

我遇到了将JSON转换为POJO的问题。我没有收到任何错误或例外。有人能指出我哪里出错吗?

您可以找到完整的代码here

以下是我的POJO:

package com.sample.restapi.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class SearchResponse {

    private List<Search> search;

    private String totalResults;

    private String response;

    public SearchResponse() {

    }

    public List<Search> getSearch() {
        return search;
    }

    public void setSearch(List<Search> search) {
        this.search = search;
    }

    public String getTotalResults() {
        return totalResults;
    }

    public void setTotalResults(String totalResults) {
        this.totalResults = totalResults;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "SearchResponse [search=" + search + ", totalResults=" + totalResults + ", response=" + response + "]";
    }

}

这是Search.java

package com.sample.restapi.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class Search {

    private String title;

    private String year;

    private String imdbID;

    private String type;

    private String poster;

    public Search() {

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getImdbID() {
        return imdbID;
    }

    public void setImdbID(String imdbID) {
        this.imdbID = imdbID;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPoster() {
        return poster;
    }

    public void setPoster(String poster) {
        this.poster = poster;
    }

    @Override
    public String toString() {
        return "Search [title=" + title + ", year=" + year + ", imdbID=" + imdbID + ", type=" + type + ", poster="
                + poster + "]";
    }
}

这是驱动程序类。

package com.sample.restapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;

import com.sample.restapi.model.SearchResponse;

@SpringBootApplication
public class ConsumerApplication {

    private static final Logger log = LoggerFactory.getLogger(ConsumerApplication.class);

    public static void main(String[] args) {

        RestTemplate restTemplate = new RestTemplate();
        SearchResponse searchResponse = restTemplate.getForObject("http://www.omdbapi.com?s=rush", SearchResponse.class);

        log.info(searchResponse.toString());
    }
}

控制台输出是:

14:34:12.941 [main] INFO com.sample.restapi.ConsumerApplication - SearchResponse [search=null, totalResults=344, response=null]

1 个答案:

答案 0 :(得分:3)

您缺少json中属性的正确标识符,大写字母和小写字母的响应和类别存在差异。在你的课程中使用@JsonProperty。

@JsonProperty("Search")
private List<Search> search = new ArrayList<Search>();

private String totalResults;
@JsonProperty("Response")
private String response;

您还应该在Search类中添加@JsonProperty注释。