Polymorphism in jackson annotations: @JsonTypeInfo usage

@JsonSubTypes.Type must have a value and a name like this,

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT, property="type")
@JsonSubTypes({       
    @JsonSubTypes.Type(value=Dog.class, name="dog"),
    @JsonSubTypes.Type(value=Cat.class, name="cat")       
}) 

In the subclass, use @JsonTypeName("dog") to say the name.
The values dog and cat will be set in the property named type.

Leave a Comment