Java – Jackson polymorphism: how to map multiple subtypes to the same class
•
Java
I'm using Jackson 1.9 x.
Suppose I have an animal class:
public class Animal { private String type; // accessors } public class Mammal extends Animal { private String diet; // accessors } public class Bird extends Animal { private boolean tropical; // accessors }
I want to do this (I map several subtypes to one class and another)
@JsonTypeInfo(use = Id.NAME,include = As.PROPERTY,property = "type") @JsonSubTypes({@JsonSubTypes.Type(value = Mammal.class,name = "Dog"),@JsonSubTypes.Type(value = Mammal.class,name = "Cat"),@JsonSubTypes.Type(value = Bird.class,name = "Dodo"},name = "Cockatoo"}) public class Animal { }
What I see now is that Jackson only recognizes the "dog to mammal" and "dodo to bird" mappings This is because stdsubtyperesolver_ Collectandresolve() allows the same class to be registered only once (due to the implementation of namedtype. Equals())
Is there a solution to the problem I see?
Solution
Maybe not using comments The problem is that this mapping does not work for serialization, and existing mappings do require a one-to-one (two-way) relationship
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码