我逐行浏览了每一行代码,但我认为这是 Jackson 在内部处理多态性的方式。
使用Dog和Cat扩展的经典示例Animal:
@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type")
@JsonTypeIdResolver(AnimalTypeIdResolver.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class Animal implements Serializable {
public AnnotatorBundleConfig(String name) {
super();
this.name = name;
}
狗类:
public class DogAnimal extends Animal {
@JsonCreator
public DogAnimal(
@JsonProperty(value="name", required=true) String name,
@JsonProperty(value="bark_decibel") int bark_decibel)
{
super(name);
this.bark_decibel = bark_decibel;}
猫类:
public class CatAnimal extends Animal {
@JsonCreator
public CatAnimal(
@JsonProperty(value="name", required=true) String name,
@JsonProperty(value="meow_level") int meow_level)
{
super(name);
this.meow_level = meow_level;}
这AnimalTypeIdResolver是一个典型的 TypeIdResolver 扩展AbstractTypeIdResolver。
出于一些非常奇怪的原因,bark_decibel并且meow_level从 JSON 反序列化,但type以null. 有任何想法吗?