I am using Jackson and I\'m having problems, when I try to deserialize an Object I get the following error:
com.fasterxml.jackson.databind.JsonMappingExcepti
In your concrete example the problem is that you don't use this construct correctly:
@JsonSubTypes({ @JsonSubTypes.Type(value = MyAbstractClass.class, name = "MyAbstractClass") })
@JsonSubTypes.Type should contain the actual non-abstract subtypes of your abstract class.
Therefore if you have:
abstract class Parent and the concrete subclasses
Ch1 extends Parent and
Ch2 extends Parent
Then your annotation should look like:
@JsonSubTypes({
@JsonSubTypes.Type(value = Ch1.class, name = "ch1"),
@JsonSubTypes.Type(value = Ch2.class, name = "ch2")
})
Here name should match the value of your 'discriminator':
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT,
property = "type")
in the property field, here it is equal to type. So type will be the key and the value you set in name will be the value.
Therefore, when the json string comes if it has this form:
{
"type": "ch1",
"other":"fields"
}
Jackson will automatically convert this to a Ch1 class.
If you send this:
{
"type": "ch2",
"other":"fields"
}
You would get a Ch2 instance.