我遍历了每一行代码,但我认为这就是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;}cat类:
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加入的。有什么想法吗?
发布于 2016-09-27 10:08:26
为@JsonTypeInfo设置visible=true:
@JsonTypeInfo(use = Id.CUSTOM, include = As.PROPERTY, property = "type", visible=true)https://stackoverflow.com/questions/39714780
复制相似问题