更新:删除多余的代码
我需要反序列化一个 JSON 对象,该对象可以是父类的许多子类型之一。具体是哪种子类型由对象中的特定枚举字段决定。我已向父类添加了 @JsonTypeInfo
和 @JsonSubTypes
注释,据我所知,它们都是正确的。
但是,枚举值与 Java 类中的枚举名称并不完全相同。因此,在枚举类中,我使用 @JsonCreator
注解了一个静态方法,以便 Jackson 知道如何将值反序列化为正确的 Java 枚举。
我遇到的问题是,一旦反序列化 JSON 字符串,保存枚举的字段不包含实际的枚举值 - 它保留为 null
。
为什么 day
字段保留为 null
以及如何在反序列化过程中按预期设置它?
代码如下。
父日历类
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "day")
// For the sake of correct deserialisation, we need to map from values of type to child model classes explicitly
@JsonSubTypes({ @JsonSubTypes.Type(value = Monday.class, name = "monday"),
@JsonSubTypes.Type(value = Monday.class, name = "mon"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tuesday"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tues")})
public class Calendar {
protected String calendarName;
protected DaysOfWeek day;
@JsonProperty("calendarName")
public String getCalendarName() {
return calendarName;
}
public void setCalendarName(String calendarName) {
this.calendarName = calendarName;
}
@JsonProperty("day")
public DaysOfWeek getDay() {
return day;
}
public void setDay(DaysOfWeek day) {
this.day = day;
}
}
周一和周二 child 类(class) 星期一
public class Monday extends Calendar {
private NegativeFeelings object;
public Monday() {
}
public Monday(NegativeFeelings object) {
this.object = object;
}
@JsonProperty("object")
public NegativeFeelings getObject() {
return object;
}
public void setObject(NegativeFeelings object) {
this.object = object;
}
}
周二
public class Tuesday extends Calendar {
private Meetings object;
public Tuesday() {
}
public Tuesday(Meetings object) {
this.object = object;
}
@JsonProperty("object")
public Meetings getObject() {
return object;
}
public void setObject(Meetings object) {
this.object = object;
}
}
DaysOfWeek 枚举类
public enum DaysOfWeek {
MONDAY("monday"),
MON("mon"),
TUESDAY("tuesday"),
TUES("tues");
private String value;
DaysOfWeek(String value) {
this.value = value;
}
@Override
@JsonValue
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static DaysOfWeek fromEventString(@JsonProperty("value") String eventString) {
return Arrays.stream(DaysOfWeek.values()).filter(e -> e.value.equals(eventString)).findFirst().get();
}
}
JSON 源字符串
{
"calendarName":"My Tuesdays",
"day":"tuesday",
"object":{
"meetings":[
"team",
"company",
"client"]
}
}
反序列化对象(从上面的字符串反序列化)
Tuesday {
object=Meetings {
meetings=[team, company, client]
},
calendarName=My Tuesdays,
day=null //I want this to be "tuesday", not null
}
最佳答案
通过同事找到了答案。
我需要将 @JsonTypeInfo
注释的 visible
属性设置为 true
:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "day", visible = true)
// For the sake of correct deserialisation, we need to map from values of type to child model classes explicitly
@JsonSubTypes({ @JsonSubTypes.Type(value = Monday.class, name = "monday"),
@JsonSubTypes.Type(value = Monday.class, name = "mon"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tuesday"),
@JsonSubTypes.Type(value = Tuesday.class, name = "tues")})
public class Calendar {
...
关于java - Jackson 反序列化子类型将枚举字段设置为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55099983/