7

コードの各行をステップ実行しましたが、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_decibelmeow_levelは JSON から逆シリアル化されていますがtypenull. 何か案は?

4

1 に答える 1

6

visible=trueに設定@JsonTypeInfo:

@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true)

この投稿を参照してください

于 2016-09-27T02:08:25.843 に答える