我遍历了每一行代码,但是我认为这就是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
进入。有任何想法吗? 最佳答案
将visible=true
设置为@JsonTypeInfo
:
@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true)
请参阅this post
关于java - jackson 的@JsonTypeInfo(use = Id.CUSTOM,include = As.PROPERTY,property = "type")读取JSON的所有字段( "type"除外),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39714780/