【发布时间】:2017-08-23 07:48:49
【问题描述】:
我有抽象类的列表
List<Messaging> data;
Messaging 是一个抽象类,其他 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"
}
如何将多态对象的列表序列化到不同的节点?
【问题讨论】: