Jackson framework provides annotation based approach to emit the type information during serialization process.
I do not want to use @JsonSubTypes annotation in my super class(Animal).
Instead I want to tell my SubClasses i.e Dog and Elephant that Animal is their parent.
Is there any approach to do so without using annotation in Animal class.
If yes, please provide example of to do the same if possible.
Following is case which I am trying to resolve.JSON which "test" receives, contains "type" field as "dog" or "elephant".
I want to register these two classes as subtypes of "Animal" class but don't want to use @JsonSubTypes in Animal.
Any help would be appreciated. Thanks in advance.
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class Animal(){
private String sound;
private String type;
//getters and setters
}
@JsonTypeName("dog")
Class Dog extends Animal(){
//some attributes.
//getters and setters
}
@JsonTypeName("elephant")
Class Elephant extends Animal(){
//some attributes.
//getters and setters
}
@Controller
public class MyController {
//REST service
@RequestMapping( value = "test")
public @ResponseBody String save(@RequestBody Animal animal){
System.out.println(animal.getClass());
return success;
}
}