我受到给定JSON结构的约束:
{
"metadata": {
"eventName": "FooEvent",
"field1": "bla"
},
"event": { ... }
}
如何使用多态反序列化和嵌套类型信息属性对其进行反序列化?我在metadata.eventName
中使用@JsonTypeInfo
嵌套属性,如下所示:
@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;
}
鉴于配置杰克逊抱怨该物业无法找到:
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]
答案 0 :(得分:2)
您在这里面临两个问题:
@JsonTypeInfo
和@JsonSubTypes
用于继承,例如class FooEventPayload extends EventPayload
。在您的情况下,EventPayload<T>
是一个通用类,杰克逊需要通过T
Look here for instance TypeReference
是什么
醇>
假设你想要一个泛型类,我建议首先序列化到树中,查看树以获取指定类型的属性,然后将树转换为此类型的对象。您可以跳过@JsonTypeInfo
和@JsonSubTypes
。 E.g。
// user object mapper to parse JSON into a tree (node is the root)
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
// use get as many times as needed by depth
// to get the value that defines the type to deserialise to
String type = node.get("metadata").get("eventName").textValue();
// convert JsonNode variable to the required type
if (type.equals("fooEvent")) {
EventPayload<FooEvent> event =
mapper.convertValue(node, new TypeReference<EventPayload<FooEvent>>(){});
} else if (type.equals("barEvent")) {
EventPayload<BarEvent> event =
mapper.convertValue(node, new TypeReference<EventPayload<BarEvent>>(){});
}