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

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 {

}

Leave a Comment