2016-03-30 10 views
0

私はこの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] 

答えて

0

物事のカップル:

まず、エラーメッセージの最後をチェックしてください。戻ってくるメディアの種類はtext/jsonです。デフォルトでは、MappingJackson2HttpMessageConverterはapplication/jsonとapplication/* + jsonのみ変換します。 text/jsonも変換するように設定する必要があります。たとえば、次のコードは、仕事ができる:

MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 

jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)); 

restTemplate.getMessageConverters().add(jsonConverter); 

第二に、私はあなたがTopStoryとジャクソンが最初にオブジェクトをインスタンス化するためのTopStoriesのデフォルトコンストラクタが必要と考えています。

参照SpringTemplate no suitable HttpMessageConverter found for response type詳細については、

+0

これは完全に機能しました。ありがとうございました! – green

関連する問題