2012-04-16 11 views
1

は、私はそれがFooのエンティティの配列があります。このJSONファイルジャクソンResourceAccessException:I/Oエラー:認識できないフィールド

[ 
    { 
     "foo":{ 
     "comment":null, 
     "media_title":"How I Met Your Mother", 
     "user_username":"nani" 
     } 
    }, 
    { 
     "foo":{ 
     "comment":null, 
     "media_title":"Family Guy", 
     "user_username":"nani" 
     } 
    } 
] 

を持っています。次のように私は私のFooTemplateを持って

その後
import org.codehaus.jackson.annotate.JsonProperty; 
    import org.codehaus.jackson.map.annotate.JsonRootName; 

    @JsonRootName("foo") 
    public class Foo { 

     @JsonProperty 
     String comment; 
     @JsonProperty("media_title") 
     String mediaTitle; 
     @JsonProperty("user_username") 
     String userName; 

/** setters and getters go here **/ 

    } 

は、その後、私は私のFooオブジェクトを持っている

public List<Foo> getFoos() { 
    return java.util.Arrays.asList(restTemplate.getForObject(buildUri("/foos.json"), 
      Foo[].class)); 
} 

をしかし、私は私の簡単なテストを実行したときに私が取得:

org.springframework.web.client.ResourceAccessException: I/O error: Unrecognized field "foo" (Class org.my.package.impl.Foo), not marked as ignorable at [Source: [email protected]; line: 3, column: 14] (through reference chain: org.my.package.impl.Foo["foo"]); 

答えて

2

Exceptionは、JSONObjectのデシリアライズを試みていることを示しています最上位の要素であるJSONArray)をFooオブジェクトに変換します。したがって、Fooエンティティの配列がない場合は、Fooメンバのオブジェクトの配列があります。ここで

ObjectMapperがやろうとしているものです。

[ 
    {   <---- It thinks this is a Foo. 
     "foo":{ <---- It thinks this is a member of a Foo. 
     "comment":null, 
     "media_title":"How I Met Your Mother", 
     "user_username":"nani" 
     } 
    }, 
    {   <---- It thinks this is a Foo. 
     "foo":{ <---- It thinks this is a member of a Foo. 
     "comment":null, 
     "media_title":"Family Guy", 
     "user_username":"nani" 
     } 
    } 
] 

それはException

Unrecognized field "foo" (Class org.my.package.impl.Foo)

を文句おそらく、あなたが最初JSONObjectを利用したいと思いますので、これのものであり、 foo識別子を取り除く。

[ 
    { 
     "comment":null, 
     "media_title":"How I Met Your Mother", 
     "user_username":"nani" 
    }, 
    { 
     "comment":null, 
     "media_title":"Family Guy", 
     "user_username":"nani" 
    } 
] 

EDIT

あなたは、代わりに、単一のFooインスタンスを保持し、その配列にアンマーシャリングしようとする新しいBarオブジェクトを作成することができます。

class Bar { 
    @JsonProperty 
    private Foo foo; 

    // setter/getter 
} 

public List<Bar> getBars() { 
    return java.util.Arrays.asList(restTemplate.getForObject(buildUri("/foos.json"), 
      Bar[].class)); 
} 
+0

残念ながら、それは "私自身の" jsonではありません。私はまた、それが壊れたjsonのようだと思ったが、それは公共のapiなので、私は "ナ..."できないようだった。おそらく私はこれらの人にそれについて尋ねるべきだと思います。多分それは実際に彼らのバグです – user1168098

+0

別の提案のために私の編集を見てください。 –

+0

大丈夫です。私はあなたの提案とFooオブジェクトプロパティを含むFooMixinオブジェクトに従った。ありがとう – user1168098

関連する問題