【问题标题】:RestTemplate returning null when serialized to POJORestTemplate 序列化为 POJO 时返回 null
【发布时间】:2017-01-31 09:27:31
【问题描述】:

我正在关注spring网站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]

【问题讨论】:

  • 究竟是什么给null?我怀疑您的意思是 List 为空。在这种情况下,stackoverflow.com/questions/19580856/… 的可能重复项
  • @Redlab 我刚刚进行了编辑,指定了我得到的控制台输出。我想你不能说它是重复的,因为我无法理解 RestTemplate 如何处理使用的 API 并转换为我的 POJO。
  • 你是对的,它不是重复的。看我的回答

标签: java spring-boot resttemplate


【解决方案1】:

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

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

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

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

【讨论】:

  • 我有一个问题。那我为什么要获取totalResults 字段的数据?
  • totalResults 大小写与 json 响应相同
  • 另外,为什么在指南中他们没有使用 @JsonProperty 注释并且它仍在工作?
  • 正如@shi 所说,类中的 totalResult 与 json 中的字符相同。如果你想添加注释,它应该是 @JsonProperty("totalResults")。
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多