提问者:小点点

通过继承实现Jackson反序列化


我有一个具有以下结构的类

<code>A类{private String类型;private T实体;}

我的JSON结构是这样的

{"type":"Relation1","entity":"{..}"} --> entity should be Relation1.class
{"type":"Relation2","entity":"{..}"}  --> entity should be Relation2.class

我怎样才能实现使用杰克逊Desilizaton的预期结果?


共1个答案

匿名用户

如果在模型中使用继承,则无法获得像显示的那样的干净 json,因为 Jackson 无法反序列化 json。如果你想使用Jackson,你必须配置ObjectMapper:

ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping();

然后您将获得一个包含反序列化过程所需的T类名的Json。

这里有一个完整的指南:指南