JSON、Jersey 和 Jackson 中的多态性
Jackson with Jersey 是否支持 JSON 上的多态类?
例如,假设我有一个 Parent 类和一个继承自它的 Child 类。并且,假设我想使用 JSON 来发送 &通过 HTTP 接收父级和子级。
public class Parent {
...
}
public class Child extends Parent {
...
}
我考虑过这种实现:
@Consumes({ "application/json" }) // This method supposed to get a parent, enhance it and return it back
public @ResponseBody
Parent enhance(@RequestBody Parent parent) {
...
}
问题:如果我给这个函数(当然通过 JSON)一个 Child 对象,它会起作用吗? Child 的额外成员字段也会被序列化吗? 基本上,我想知道这些框架是否支持多态消费和多态消费。回应。
顺便说一句,我正在使用 Spring MVC。
Does Jackson with Jersey support polymorphic classes over JSON?
Let's say, for instance, that I've got a Parent class and a Child class that inherits from it. And, let's say I want to use JSON to send & receive both Parent and Child over HTTP.
public class Parent {
...
}
public class Child extends Parent {
...
}
I've thought about this kind of implementation:
@Consumes({ "application/json" }) // This method supposed to get a parent, enhance it and return it back
public @ResponseBody
Parent enhance(@RequestBody Parent parent) {
...
}
Question: If I give this function (through JSON of course) a Child object, will it work? Will the Child's extra member fields be serialized, as well ?
Basically, I want to know if these frameworks support polymorphic consume & respond.
BTW, I'm working with Spring MVC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jackson确实支持多态性,
在你的子类中用名称注释:
在父类中你指定子类型:
Jackson does support polymorphism,
In your child class annotate with a name:
In parent you specify sub types: