如何使用RestTemplates和FasterXml-Jackson正确解析json数据?

时间:2016-03-30 20:17:59

标签: java json spring

我有这个json字符串: {“status”:“OK”,“copyright”:“Copyright(c)2016 The New York Times Company。保留所有权利。”,“section”:“home”,“last_updated”:“2016-03-30T16: 02:01-05:00“,”num_results“:19,”结果“:[{”section“:”Health“,...}]

我只需要结果,这是纽约时报的头条新闻。

这是我的TopStory.class

@JsonIgnoreProperties(ignoreUnknown = true)
public class TopStory {

    private String title;
    @JsonProperty("abstract")
    private String storyAbstract;
    private String url;
    private String section;
    private String published_date;

    public String getTitle() {
        return title;
    }

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

    public String getStoryAbstract() {
        return storyAbstract;
    }

    public void setStoryAbstract(String storyAbstract) {
        this.storyAbstract = storyAbstract;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getPublished_date() {
        return published_date;
    }

    public void setPublished_date(String published_date) {
        this.published_date = published_date;
    }

    public TopStory(String title, String storyAbstract, String url, String section, String published_date) {

        this.title = title;
        this.storyAbstract = storyAbstract;
        this.url = url;
        this.section = section;
        this.published_date = published_date;
    }
}

这是我的TopStories.class

@JsonIgnoreProperties(ignoreUnknown = true)
public class TopStories {

    @JsonProperty("results")
    private ArrayList<TopStory> stories;

    public TopStories(ArrayList<TopStory> stories) {
        this.stories = stories;
    }

    public ArrayList<TopStory> getStories() {
        return stories;
    }

    public void setStories(ArrayList<TopStory> stories) {
        this.stories = stories;
    }
} 

这是我在主类中使用的代码:

RestTemplate restTemplate=new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ArrayList<TopStory> topStories=restTemplate.getForObject(url,TopStories.class).getStories();

我一直得到这个例外。我该如何解决这个问题?

    org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class TopStories] and content type [text/json]

1 个答案:

答案 0 :(得分:0)

有几件事:

首先,检查错误消息的结尾;它说回来的媒体类型是text / json。事实证明,默认情况下MappingJackson2HttpMessageConverter只转换application / json和application / * + json。您需要将其配置为也转换text / json。例如,以下代码可以工作:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EventViewModel model) {
    // ........

    // I had to add this line to get this piece to save
    db.Entry(model.Event.ProviderFeedback).State = EntityState.Modified;
    db.Entry(model.Event).State = EntityState.Modified;

    await db.SaveChangesAsync();

    // ........

}

其次,我相信你需要TopStory的默认构造函数和Jackson的TopStories来初始实例化对象。

参考SpringTemplate no suitable HttpMessageConverter found for response type了解更多细节。