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

后端 未结 2 918
耶瑟儿~
耶瑟儿~ 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条回答
  • 2020-12-05 08:06

    There is a simpler annotation out now:

    @JsonRootName("dog")
    public class Dog extends Animal {...}
    

    The reference to the annotation can be found on fasterxml.github

    0 讨论(0)
  • 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 {
    
    }
    
    0 讨论(0)
提交回复
热议问题