2016-10-20 8 views
2

外部NYtimes APIを使用しようとしています。ここではサンプル例データ出力です:いいえ文字列値からデシリアライズする文字列引数コンストラクタ/ファクトリメソッド

{ 
    "status": "OK", 
    "copyright": "Copyright (c) 2016 The New York Times Company. All Rights Reserved.", 
    "section": "home", 
    "last_updated": "2016-10-20T19:57:45-04:00", 
    "num_results": 30, 
    "results": [{ 
      "section": "Briefing", 
      "subsection": "", 
      "title": "Election, Dodgers, Duterte: Your Thursday Evening Briefing", 
      "abstract": "Here’s what you need to know at the end of the day.", 
      "url": "abc.co", 
      "byline": "By KAREN ZRAICK and MERRILL D. OLIVER", 
      "item_type": "Article", 
      "updated_date": "2016-10-20T18:32:41-04:00", 
      "created_date": "2016-10-20T17:58:30-04:00", 
      "published_date": "2016-10-20T17:58:30-04:00", 
      "material_type_facet": "", 
      "kicker": "", 
      "des_facet": [], 
      "org_facet": [], 
      "per_facet": [], 
      "geo_facet": [], 
      "multimedia": [{ 
       "url": "abc.co", 
       "format": "Standard Thumbnail", 
       "height": 75, 
       "width": 75, 
       "type": "image", 
       "subtype": "photo", 
       "caption": "", 
       "copyright": "Damon Winter/The New York Times" 
      }, { 
       "url": "abc.co", 
       "format": "thumbLarge", 
       "height": 150, 
       "width": 150, 
       "type": "image", 
       "subtype": "photo", 
       "caption": "", 
       "copyright": "Damon Winter/The New York Times" 
      }, { 
       "url": "abc.co", 
       "format": "Normal", 
       "height": 127, 
       "width": 190, 
       "type": "image", 
       "subtype": "photo", 
       "caption": "", 
       "copyright": "Damon Winter/The New York Times" 
      }, { 
       "url": "abc.co", 
       "format": "mediumThreeByTwo210", 
       "height": 140, 
       "width": 210, 
       "type": "image", 
       "subtype": "photo", 
       "caption": "", 
       "copyright": "Damon Winter/The New York Times" 
      }, { 
       "url": "abc.co", 
       "format": "superJumbo", 
       "height": 1365, 
       "width": 2048, 
       "type": "image", 
       "subtype": "photo", 
       "caption": "", 
       "copyright": "Damon Winter/The New York Times" 
      }], 
      "short_url": "http://abc.co" 
     } 

    ] 
} 

ここでBean定義

//Headline.class 
public class Headline { 

@JsonProperty("status") 
String status; 
@JsonProperty("copyright") 
String copyright; 
@JsonProperty("section") 
String section; 
@JsonProperty("last_updated") 
String last_updated; 
@JsonProperty("num_results") 
int num_results; 
@JsonProperty("results") 
List<Story> results; 

`

//Story.class 
    @JsonProperty("section") 
     String section; 
     @JsonProperty("subsection") 
     String subsection; 
     @JsonProperty("title") 
     String title; 
     @JsonProperty("abstract") 
     String storyAbstract; 
     @JsonProperty("url") 
     String url; 
     @JsonProperty("byline") 
     String byline; 
     @JsonProperty("item_type") 
     String item_type; 
     @JsonProperty("updated_date") 
     String updated_date; 
     @JsonProperty("created_date") 
     String created_date; 
     @JsonProperty("published_date") 
     String published_date; 
     @JsonProperty("material_type_facet") 
     String material_type_facet; 
     @JsonProperty("kicker") 
     String kicker; 
     /*@JsonProperty("des_facet") 
     List des_facet; 
     @JsonProperty("org_facet") 
     List org_facet; 
     @JsonProperty("per_facet") 
     List per_facet; 
     @JsonProperty("geo_facet") 
     List geo_facet;*/ 
     @JsonProperty("multimedia") 
     List<Media> multimedia; 
     @JsonProperty("short_url") 
     String short_url; 

//Media.class 
     @JsonProperty("url") 
     String url; 
     @JsonProperty("format") 
     String format; 
     @JsonProperty("height") 
     int height; 
     @JsonProperty("width") 
     int width; 
     @JsonProperty("type") 
     String type; 
     @JsonProperty("subtype") 
     String subtype; 
     @JsonProperty("caption") 
     String caption; 
     @JsonProperty("copyright") 
     String copyright; 

はここに私の発信者であるされています

RestTemplate restTemplate = new RestTemplate(); 
     MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); 
     jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("text","json",MappingJackson2HttpMessageConverter.DEFAULT_CHARSET))); 
     restTemplate.getMessageConverters().add(jsonConverter); 
     Headline headline = restTemplate.getForObject("http://abc/svc/topstories/v1/home.json?api-key=key",Headline.class); 
     logger.info("Headline"+headline.toString()); 

例外になりました私がCを作るときに私が得ることすべてがある:私はそれをデシリアライズしていない理由を理解しようとしてきた

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not construct instance of java.util.ArrayList: no String-argument constructor/factory method to deserialize from String value ('') 
at [Source: [email protected]; line: 1, column: 1185] (through reference chain: NYTimes.Payload.Headline["results"]->java.util.ArrayList[0]->NYTimes.Story.Story["multimedia"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.ArrayList: no String-argument constructor/factory method to deserialize from String value ('') 
at [Source: [email protected]; line: 1, column: 1185] (through reference chain: NYTimes.Payload.Headline["results"]->java.util.ArrayList[0]->NYTimes.Story.Story["multimedia"]) 

。文字列の値が空の場合、null値が返されます。

+1

なぜあなたは正しいJSONにしませんか?角かっこで囲み、何が起こるかを見てください。 – Jezor

+0

これを正しいJSONで更新しました。 –

+0

@HarryGohlでコードを実行すると、何か変更がありましたか? – Jezor

答えて

-1

この例外は、理由はあなたが受けているJSONレスポンスの発生している -

{ 
    "status": "OK", 
    "copyright": "Copyright (c) 2016 The New York Times Company. All Rights Reserved.", 
    "section": "home", 
    "last_updated": "2016-10-20T19:57:45-04:00", 
    "num_results": 30, 
    "results": [{ 
      "section": "Briefing", 
      "subsection": "", 
      "title": "Election, Dodgers, Duterte: Your Thursday Evening Briefing", 
      "abstract": "Here’s what you need to know at the end of the day.", 
      "url": "abc.co", 
      "byline": "By KAREN ZRAICK and MERRILL D. OLIVER", 
      "item_type": "Article", 
      "updated_date": "2016-10-20T18:32:41-04:00", 
      "created_date": "2016-10-20T17:58:30-04:00", 
      "published_date": "2016-10-20T17:58:30-04:00", 
      "material_type_facet": "", 
      "kicker": "", 
      "des_facet": [], 
      "org_facet": [], 
      "per_facet": [], 
      "geo_facet": [], 
      "multimedia": "", 
      "short_url": "http://abc.co" 
     } 

    ] 
} 

「マルチメディア」フィールドが空の文字列

ですつまり、あなたのJSONを確認し、あなたをマッピングする必要がありますそれに応じてpojo。

+0

デリックに感謝します。しかし、問題は私がデータを制御しないことです - 空の文字列をどうやって扱うのですか? –

+0

これはあなた自身のカスタムデシリアライザを書く必要があります。これは参考になるかもしれません - http://stackoverflow.com/questions/12933394/jackson-deserialize-as-an-empty-list –

関連する問題