我有抽象类列表
List<Messaging> data;
消息传递是一个抽象类,其他 3 个类正在扩展这个类。
例如
Message class extends Messaging
Product class extends Messaging
Filter class extends Messaging
我正在添加上述列表中的所有消息
我在消息传递类中使用了@JsonTypeInfo,如下所示:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@Type(value = Message.class, name = "message"),
@Type(value = Filter.class, name = "filter"),
@Type(value = Product.class , name = "product")})
当我使用 jackson 将此 Java 对象序列化为 JSON 时,我得到的值如下:
{
"@type": "message",
"type": "text",
"value": "Is this the information you need"
}
我希望我的价值如下:
"message" : {
"type": "text",
"value": "Is this the information you need"
}
如何将多态对象列表序列化到不同的节点?
更改JsonTypeInfo
以包含为WRAPPER_OBJECT
。
例如:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT)