为什么 JAXB 不编组我自己的子类
我有一个可以成功编组的 jaxb 对象,它有一个列表对象,然后我创建一个如下所示的新对象
public class Sub extends SuperJAXBClass{
@Override
public List getList1(){
//override here
return ...;
}
}
然后代码如下: SuperJAXBClass sjc=new Sub(); marshall(sjc)
然后我发现Sub中的List1没有编组成功。 任何人都知道为什么会发生这种情况? 怎么解决呢?
I have a jaxb object which can be marshalled successfully, and it has a list object, then I make a new object like below
public class Sub extends SuperJAXBClass{
@Override
public List getList1(){
//override here
return ...;
}
}
Then the code like below:
SuperJAXBClass sjc=new Sub();
marshall(sjc)
Then I found the List1 in Sub is not marshalled successfully.
Any one knows why this happens?
How to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作之一:
选项 1 -
@XmlSeeAlso
注释JAXB (JSR-222) 实现无法使用 Java 反射来确定所有可能的子类。作为解决方法,您可以使用
@XmlSeeAlso
注释来注释超类,该注释为 JAXB 提供对子类的引用。选项 #2 - 创建 JAXBContext 时传递子类
如果您在创建
JAXBContext
时包含子类,那么 JAXB 实现将会意识到它。当子类传入超类的元数据时,也会创建超类。You could do one of the following:
Option #1 -
@XmlSeeAlso
AnnotationJAXB (JSR-222) implementations can not use Java reflection to determine all the possible suclasses. As a work around you can annotate the super class with the
@XmlSeeAlso
annotation that provides JAXB a reference to the subclasses.Option #2 - Pass the Subclass When Creating JAXBContext
If you include the subclass when creating the
JAXBContext
then JAXB implementations will be aware of it. When a subclass is passed in metadata for the super classes is also created.