I have this configuration on the parent class:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "type",
visible = true
)
@JsonSubTypes({
@JsonSubTypes.Type(value = AnalysisViewer.class, name = "ANALYSIS"),
@JsonSubTypes.Type(value = CombinedAnalysisViewer.class, name = "COMBINED"),
@JsonSubTypes.Type(value = SingleSourceViewer.class, name = "SINGLESOURCE"),
@JsonSubTypes.Type(value = SingleSourceGroupViewer.class, name = "SINGLESOURCE_GROUP")
})
And the class has the following type
attribute that I need to persist in the database (with JPA).
public class Viewer {
...
@Column(name = "TP_VIEWER")
@Enumerated(EnumType.STRING)
private ViewerTypeEnum type;
...
}
With this config or changing to visible=false
and include=JsonTypeInfo.As.PROPERTY
I'm either getting an error on serialization saying that it's not possible to write twice the same property, or an error saying that I can't save a null value in the type
column.
What I need is to have the type attribute in the database AND use it to teach Jackson the subtypes. I tried a lot of different configurations, and I can't find documentation on keeping and populating the property on deserialization. Can someone help me with that?
Thanks in advance.
Add parameter visible
to @JsonTypeInfo
and make it true
. By default it is false
:
@JsonTypeInfo(..., visible = true)
From the JsonTypeInfo documentation:
Property that defines whether type identifier value will be passed as part of JSON stream to deserializer (true), or handled and removed by TypeDeserializer (false). Property has no effect on serialization.
Default value is false, meaning that Jackson handles and removes the type identifier from JSON content that is passed to JsonDeserializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With