我想用Jackson反序列化一个JSON对象.因为目标是一个接口,所以我需要指定应该使用哪个实现.

This information could be stored in the JSON-Object, using @JsonTypeInfo-Annotation. But I want to specify the implementation in source code because it's always the same.

Is this possible?

推荐答案

Use a SimpleAbstractTypeResolver:

ObjectMapper mapper = new ObjectMapper();

SimpleModule module = new SimpleModule("CustomModel", Version.unknownVersion());

SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
resolver.addMapping(Interface.class, Implementation.class);

module.setAbstractTypes(resolver);

mapper.registerModule(module);

Json相关问答推荐

JoltChange:将输入数组的每个对象拆分为2个独立的对象,并将其放入输出数组中

在Jenkins中使用ReadJSON读取json子元素

PowerShell脚本未按预期生成预期的JSON输出

如何创建可由Gin序列化到json的排序键值映射?

JQ-JSON将键转换为对象

Jolt-在数组列表中插入新的字段

使用JQ合并JSON列表中的对象

根据值过滤输入的JSON并使用它准备预期的输出

JOLT 获取带有动态键的数组

Powershell - 如何从 JSON 中删除/过滤元素但保留其余部分?

判断golang中解析的json响应中是否存在所需的json键(不是值)

流编写器未写入 webapi 中的 JSON 文件

传统编程语言等价于动态 SQL

将文本转换为 python 列表

UTF-8 字符编码之战 json_encode()

如何从 rails 中的 respond_to 方法生成 json?

如何将 LinkedTreeMap 转换为 gson JsonObject

我们可以使用 JSON 作为数据库吗?

在 React 中访问子级的父级状态

MySQL Select JSON 字段属性具有值的位置