![](/img/trans.png)
[英]Jackson polymorphic deserialization with type property that is nested in object
[英]polymorphic deserialization of JSON with jackson, property type becomes “null”
我正在使用Jackson將一個大的Json字符串轉換為各種類和子類。
我有一個對象列表,每個對象包含一個節點對象,一個最后的結果對象和一個子數組。 children數組包含具有完全相同設置的對象列表。 這持續3或4層。
每個層的節點都是不同的子類,它們都是從節點超類擴展而來的。 我用以下注釋注釋了超類節點:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Type1ResponseDto.class, name = "Type1"),
@JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
@JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
@JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4"),
@JsonSubTypes.Type(value = Type5ResponseDto.class, name = "Type5")
})
這似乎有效,因為所有子類都被映射。
但是,這會以某種方式導致“type”屬性設置為null。
關於為什么會這樣的想法?
我需要為要顯示的type屬性添加visible=true
:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type",visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Type1ResponseDto.class,name = "Type1"),
@JsonSubTypes.Type(value = Type2ResponseDto.class, name = "Type2"),
@JsonSubTypes.Type(value = Type3ResponseDto.class, name = "Type3"),
@JsonSubTypes.Type(value = Type4ResponseDto.class, name = "Type4")
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.