我遍历了每一行代码,但是我认为这就是Jackson内部处理多态性的方式。

使用DogCat扩展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_decibelmeow_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/

10-13 04:24