I have polymorphic types and serialization when the type parameter is passed in JSON works fine.
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", defaultImpl = Dog.class)
@JsonSubTypes({
@JsonSubTypes.Type(value=Dog.class, name="dog"),
@JsonSubTypes.Type(value=Cat.class, name="cat")
})
public class Animal { ... }
public class Dog extends Animal { ... }
public class Cat extends Animal { ... }
{"name":"Tom", type="cat"} // this works as expected
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response animal(Animal animal) {...}
But, if I directly want to get a subtype object without passing type it does not work.
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response dog(Dog dog) {...}{"name":"Tom"} // this wont work
Missing type id when trying to resolve subtype of [simple type, class ....Dog]: missing type id property 'type'
Any way to achieve this?