【发布时间】:2020-11-27 05:53:06
【问题描述】:
我有一个如下所示的 MySQL 表
-------------------------------------------------------------------------
| id | name | type | properties |
| 1 | nameA | A | {"prop_a1" : "value_1", "prop_a2": "value_2"} |
| 2 | nameB1 | B | {"prop_b1" : "value_3"} |
| 3 | nameB2 | B | {"prop_b1" : "value_4"}. |
-------------------------------------------------------------------------
Class BaseEntity implements Serializable {
}
Class EntityA exends BaseEntity {
String prop_a1;
String prop_a2;
}
class EntityB extends BaseEntity {
String prop_b1;
}
class ParentEntity<T extends BaseEntity> {
Integer id;
String name;
Enum type;
T properties;
}
我试图根据类型列将多态对象保留在属性列中。
尝试使用JsonTypeInfo,虽然它能够写入 MySQL 表但在读取时失败并出现错误:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field
以前有人处理过这个用例吗?
【问题讨论】: