Polymorphism in jackson annotations: @JsonTypeInfo usage

后端 未结 2 1838
醉话见心
醉话见心 2020-11-27 14:32

I would like to know if @JsonTypeInfo annotation can be used for interfaces. I have set of classes which should be serialized and deserialized.

Here is

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 14:46

    @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.

提交回复
热议问题