Parent.java has
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
defaultImpl = com.common.model.Parent.class,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "@class"
)
@JsonSubTypes({
@JsonSubTypes.Type(value = com.common.model.Child.class, name = "com.common.model.Child"),
@JsonSubTypes.Type(value = com.common.model.Grandchild.class, name = "com.common.model.Grandchild")
})
Child.java has
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
defaultImpl = com.common.model.Parent.class,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = "@class"
)
@JsonSubTypes(@JsonSubTypes.Type(value = com.common.model.Grandchild.class, name = "com.common.model.Grandchild"))
Note: in Child.java I mentioned defaultImpl as the Parent class as Child class is abstract. My aim is to create a parent object if @class is missing in the payload.
But when I try to create the child object, I get the below error:
JSON parse error: Missing type id when trying to resolve subtype of [simple type, class com.common.model.Child]: missing type id property '@class'; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class com.common.model.Child]: missing type id property '@class'\n at [Source: (PushbackInputStream); line: 3, column: 1]
I tried giving defaultImpl = Object.class in JsonTypeInfo of Child class. Still I get the same missing type id property '@class' error.
So, my question is, is it mandatory for defaultImpl to be on the same class and any of the children class?
Can I not give any random or the parent class in defaultImpl ?
Spring boot version is 2.1.5.