私はこのjson文字列を持っています: {"ステータス": "OK"、 "copyright": "Copyright(c)2016 The New York Times Company。All Rights Reserved。"、 "section": "home"、 "last_updated ":" 2016-03-30T16:02:01-05:00 "、" num_results ":19、" results ":[{"セクション ":"健康 "、...}]RestTemplatesとFasterXml-Jacksonでjsonデータを正しく解析するには?
ニューヨークタイムズのトップストーリーである結果が必要です。
ここではこれは私がメインクラスで使用したコードである私の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]
これは完全に機能しました。ありがとうございました! – green