2016-08-09 141 views
0

產生JSON這是我的代碼部分:奇怪行爲的研究,從對象

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "OAType", propOrder = { 
    "cDefCamelTDPDataList", 
}) 
public class OAType { 

    @XmlElement(name = "c-def-camel-TDP-data-list", required = true) 
    protected cDefCamelTDPDataListType cDefCamelTDPDataList; 
    ... 

當我創建JSON有:

ObjectMapper mapper = new ObjectMapper(); 
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); 
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); 

mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tag); // Object tag 

我得到:

{ 
"cDefCamelTDPDataList" : { }, 
"cdefCamelTDPDataList" : { }, 
... 

我沒有」不明白錯在哪裏,Jackson爲什麼創建2次cDefCamelTDPDataList字段?

回答

0

您已啓用Jackson使用字段。但是默認情況下,getter/setter也被啓用。我假設你有一個名爲getCdefCamelTDPDataList()的獲得者。

如果你只想處理,你必須先禁用的getter/setter方法領域:

mapper.setVisibility(JsonMethod.ALL, Visibility.NONE); // disable getters/setters/is... 
mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);