【发布时间】:2019-09-14 13:37:56
【问题描述】:
当使用 Jackson Datamapper 反序列化我的 yaml-config 时,配置中用于决定创建什么子类型的值会被忽略,而不是保存在创建的 java 对象中。
这是我正在尝试读取的配置...
#config
---
lists:
- filename: filename.xlsx
template: filename_template.xlsx
type: classType #this value is ommitted
...
有了这个抽象类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Example.class, name = "Example"),
@JsonSubTypes.Type(value = OtherExample.class, name = "OtherExample")
})
public abstract class Report {
private String filename;
private String template;
private String type; //this value is always null
public Report() {
}
并创建和调用 ObjectMapper:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.enableDefaultTyping();
String 类型应该是 JsonSubType 选项用来设置类型的值之一,而不是 null。
【问题讨论】:
标签: java jackson yaml deserialization