3
コードの各行を踏んでいますが、多形性を内部的に有する。Jacksonの@JsonTypeInfo(use = Id.CUSTOM、include = As.PROPERTY、property = "type")はJSONの "type"以外のすべてのフィールドを読み込みます
@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あるDog
とAnimal
を拡張Cat
の典型的な例を用い
そのexte nds AbstractTypeIdResolver
。
いくつかの非常に奇妙な理由で、bark_decibel
とmeow_level
はJSONから直列化復元されているが、type
はnull
としてつつあります。何か案は? @JsonTypeInfo
ため