Spring @RequestBody containing a list of different types (but same interface)

后端 未结 2 919
耶瑟儿~
耶瑟儿~ 2020-12-05 07:44

Let\'s say that I have a domain class :

    public class Zoo{
        private List animals;
        ....

where an Animal is a

2条回答
  •  -上瘾入骨i
    2020-12-05 08:15

    You should use the Jackson annotations @JsonTypeInfo and @JsonSubTypes to achieve polymorphic json. The annotations go on the Animal base class.

    @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "type")
    @JsonSubTypes({@JsonSubTypes.Type(value = Dog.class, name = "Dog"),
            @JsonSubTypes.Type(value = Cat.class, name = "Cat")})
    public abstract class Animal {
    
    }
    

提交回复
热议问题