Java – polymorphic deserialization of JSON using Jackson, and the property type becomes “null”
•
Java
I'm using Jackson to convert a large JSON string into categories and subclasses
I have a list of objects. Each object contains a node object, a final result object and a sub array The children array contains a list of objects with exactly the same settings This lasts 3 or 4 layers
The nodes of each layer are different subclasses, which are extended from the node superclass I annotated the superclass node with the following comments:
@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") })
This seems to work because all subclasses are mapped
However, this will cause the "type" attribute to be set to null in some way
Why do you think so?
Solution
I need to add visible = true for the type attribute to be displayed:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.EXISTING_PROPERTY,property = "type",visible = true) @JsonSubTypes({ @JsonSubTypes.Type(value = Type1ResponseDto.class,name = "Type1"),name = "Type4") })
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码