2014-09-24 71 views
0

這似乎是一個簡單的實現,但不知何故不適合我。對於子對象列表JSON:UnrecognizedPropertyException

public class ParentEntity { 

    private List<ChildEntity> childFields; 

    public List<ChildEntity> getChildFields() { 
     return childFields; 
    } 

    public void setChildFields(List<ChildEntity> childFields) { 
    this.childFields = childFields; 
    } 

} 

輸入JSON

{ 
"childFields": [ 
    {<different child properties>}, 
    {<different child properties>} 
    ] 
} 

異常

class ChildEntity not marked as ignorable (11 known properties:...different child field properties 
+0

對我來說看起來是正確的,你可以在閱讀你的JSON的地方分享你的代碼嗎? – 2014-09-24 15:24:42

+0

'private List childFields = new ArrayList <>();'? – 2014-09-24 15:34:31

回答

1

關於你加入了一個異常消息,你的屬性,你在你的JSON指定的不匹配ChildEntity和ChildEntity屬性。

如果你有一個不匹配,並要指定在JSON更多的屬性,比ChildEntity類可用,您可以使用傑克遜的

@JsonIgnoreProperties 

註解。它會忽略你在POJO中沒有定義的每個屬性。

你也可以選擇使用:

ObjectMapper objectMapper = getObjectMapper(); 
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

它會忽略未聲明的所有屬性。