'Deserialize object with polymorphic types

I know this question has been asked before but I am having trouble applying it to my use case. I have the following classes, and I can't change the fields (because this is an existing system and I am trying to reuse the models):

public class Jacket {

    private Button button;
}
@XmlSeeAlso({ Zipper.class, Drawstring.class })
public abstract class Button {

    private String color;
}


public class Zipper extends Button {

    private String metalType;

}

public class Drawstring extends Button {

    private int stringLength;
}

Now, I want to be able to deserialize json so that if I have `"button":"aluminum", it gets deserialized correctly into Zipper, although the field name was "button", because when I create the Jacket object, the field name is button no matter if I am using Zipper or Drawstring.

I have tried the following annotations to the Button class:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(value = Zipper.class, name = "Zipper"), @JsonSubTypes.Type(value = Drawstring.class, name = "Drawstring") })

But I get an error: Could not resolve subtype of [simple type, class Button]: missing type id property '@type' (for POJO property 'button') at ...)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source