I'm constrained by the given JSON structure:
{
"metadata": {
"eventName": "FooEvent",
"field1": "bla"
},
"event": { ... }
}
How can I deserialize it using polymorphic deserialization and nested type info property? I'm using metadata.eventName
nested property in @JsonTypeInfo
like this:
@JsonTypeInfo(
use = Id.NAME,
include = As.EXISTING_PROPERTY,
visible = true,
property = "metadata.eventName"
)
@JsonSubTypes({
@Type(name="fooEvent", value = FooEvent.class)
@Type(name="barEvent", value = BarEvent.class)
})
public class EventPayload<T> {
private Metadata metadata;
private T event;
}
Given that config Jackson complains the property cannot be found:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'metadata.eventName' that is to contain type id (for class EventPayload)
at [Source: {
"metadata": {
"eventName": "FooEvent",
"field1": "bla"
},
"content": { ... }
}; line: 16, column: 1]