java 杰克逊反序列化多种类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32766922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson deserialization on multiple types
提问by erp
I have an abstract class called Instance
and then two implementations of that, UserInstance
and HardwareInstance
. The issue I am having is that when I call the rest endpoint for a @POST
into the database, I ideally wanted it to be like .../rest/soexample/instance/create
where the instance is passed to the REST endpoint. If Instance
wasn't abstract with more than one implementation it would be fine, but since I have 2 I am getting a Hymanson.databind
error.
我有一个名为的抽象类Instance
,然后是它的两个实现,UserInstance
和HardwareInstance
. 我遇到的问题是,当我将 a 的其余端点调用@POST
到数据库中时,我理想地希望它像.../rest/soexample/instance/create
实例被传递到 REST 端点的位置一样。如果Instance
不是具有多个实现的抽象,那就没问题了,但是由于我有 2 个,因此Hymanson.databind
出现错误。
" problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information"
“问题:抽象类型要么需要映射到具体类型,要么使用自定义反序列化器,要么使用附加类型信息进行实例化”
After looking up a solution to this I found a SO answer that said I could use something like:
在查找了解决方案后,我找到了一个 SO 答案,上面说我可以使用以下内容:
@JsonDeserialize(as=UserInstance.class)
@JsonDeserialize(as=UserInstance.class)
But it seem's like that isonly useful if there is one implementation of the abstract class. Assuming I can't call it twice since there would be no way for it to decide which type of instance it would be.
但似乎只有在抽象类的一种实现时才有用。假设我不能调用它两次,因为它无法决定它是哪种类型的实例。
So I am wondering what is the best way to handle this situation? Should I create different endpoints? Like:
所以我想知道处理这种情况的最佳方法是什么?我应该创建不同的端点吗?喜欢:
.../rest/soexample/userinstance/create
& .../rest/soexample/hardwareinstance/create
.../rest/soexample/userinstance/create
& .../rest/soexample/hardwareinstance/create
I am not too sure as I am a noobie @ REST related things, though actively trying to learn. Thanks!
我不太确定,因为我是一个菜鸟@REST 相关的东西,虽然积极尝试学习。谢谢!
回答by carcaret
Here is what I did in your same case:
这是我在同一个案例中所做的:
@JsonDeserialize(using = InstanceDeserializer.class)
public abstract class Instance {
//.. methods
}
@JsonDeserialize(as = UserInstance.class)
public class UserInstance extends Instance {
//.. methods
}
@JsonDeserialize(as = HardwareInstance.class)
public class HardwareInstance extends Instance {
//.. methods
}
public class InstanceDeserializer extends JsonDeserializer<Instance> {
@Override
public Instance deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = (ObjectNode) mapper.readTree(jp);
Class<? extends Instance> instanceClass = null;
if(checkConditionsForUserInstance()) {
instanceClass = UserInstance.class;
} else {
instanceClass = HardwareInstance.class;
}
if (instanceClass == null){
return null;
}
return mapper.readValue(root, instanceClass );
}
}
You annotate Instance
with @JsonDeserialize(using = InstanceDeserializer.class)
to indicate the class to be used to deserialize the abstract class. You need then to indicate that each child class will be deserialized as
themselves, otherwise they will use the parent class deserializer and you will get a StackOverflowError
.
您Instance
用 with@JsonDeserialize(using = InstanceDeserializer.class)
进行注释以指示用于反序列化抽象类的类。然后您需要指出每个子类将as
自己反序列化,否则他们将使用父类反序列化器,您将获得StackOverflowError
.
Finally, inside the InstanceDeserializer
you put the logic to deserialize into one or another child class (checkConditionsForUserInstance()
for example).
最后,在里面InstanceDeserializer
你把反序列化的逻辑放到一个或另一个子类中(checkConditionsForUserInstance()
例如)。