本文介绍了基于Enum的Jackson多态反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用,这是我的代码,它反序列化为基于'type'属性:
I'm working with JacksonPolymorphicDeserialization, this is my code which deserializes into the proper class based in the 'type' property:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type",
defaultImpl = Event.class,
visible = true)
@JsonSubTypes({
@Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),
@Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),
})
public abstract class AbstractEvent {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
它工作正常,我的json变成了预期的类根据'type'值。
It's working perfectly and my json turns into the expected class according to the 'type' value.
但是,我正在考虑将'type'属性从String移动到Enum,这是我的新代码:
However, I'm considering to move the 'type' property from String to Enum, this is my new code with this change:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type",
defaultImpl = Event.class,
visible = true)
@JsonSubTypes({
@Type(value = SpecialEvent1.class, name = "SPECIAL_EVENT_1"),
@Type(value = SpecialEvent2.class, name = "SPECIAL_EVENT_2"),
})
public abstract class AbstractEvent {
private EventType type;
public EventType getType() {
return type;
}
public void setType(EventType type) {
this.type = type;
}
}
和枚举:
public enum EventType {
SPECIAL_EVENT_1,
SPECIAL_EVENT_2,
EVENT;
}
问题是第二种方法不起作用......任何想法为什么???我可以在这里使用Enum吗?
The problem is that this second approach is not working... any idea why??? can I use Enum here???
谢谢!
推荐答案
固定!
适用于jackson 2.0 !!
It works with jackson 2.0!!
这篇关于基于Enum的Jackson多态反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!