我逐行检查了每一行代码,但我认为这是 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
是扩展 AbstractTypeIdResolver
的典型 TypeIdResolver .
出于一些非常奇怪的原因,bark_decibel
和 meow_level
是从 JSON 反序列化的,但是 type
正在以 null
的形式进入.有什么想法吗?
最佳答案
为@JsonTypeInfo
设置visible=true
:
@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true)
关于java - Jackson 的 @JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type") 读取除了 "type"以外的所有 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714780/