Serialization with Polymorphisme and EXTERNAL_PROPERTY = duplicate property (original) (raw)
With this kind of POJO :
public class Cage
{
public String id;
public String type;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type",
include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "CAT"),
@JsonSubTypes.Type(value = Dog.class, name = "DOG")
})
public Animal animal;
}
public abstract class Animal { }
public class Cat extends Animal {
public String firstName = "My name is cat";
}
public class Dog extends Animal {
public String lastName = "My name is dog";
}When I serialize this :
Cage cage = new Cage(); cage.id = "123"; cage.type = "CAT"; cage.animal = new Cat(); String xml1 = MAPPER.writeValueAsString(cage); System.out.println(xml1);
I get the following result (the property type is duplicated) :
Wouldn't be the expected result ?
My name is cat 123 CAT