Java: Jackson polymorphic JSON deserialization of an object with an interface property? Java: Jackson polymorphic JSON deserialization of an object with an interface property? json json

Java: Jackson polymorphic JSON deserialization of an object with an interface property?


You should use JsonTypeInfo.As.EXTERNAL_PROPERTY instead of JsonTypeInfo.As.PROPERTY. In this scenario your Asset class should look like this:

class Asset {    @JsonTypeInfo(            use = JsonTypeInfo.Id.NAME,            include = JsonTypeInfo.As.EXTERNAL_PROPERTY,            property = "type")    @JsonSubTypes({        @JsonSubTypes.Type(value = ImageAssetProperties.class, name = "image"),        @JsonSubTypes.Type(value = DocumentAssetProperties.class, name = "document") })    private AssetProperties properties;    public AssetProperties getProperties() {        return properties;    }    public void setProperties(AssetProperties properties) {        this.properties = properties;    }    @Override    public String toString() {        return "Asset [properties("+properties.getClass().getSimpleName()+")=" + properties + "]";    }}

See also my answer in this question: Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected.