为什么 JAXB 不编组我自己的子类

发布于 2025-01-04 17:05:20 字数 302 浏览 3 评论 0原文

我有一个可以成功编组的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2025-01-11 17:05:20

您可以执行以下操作之一:

选项 1 - @XmlSeeAlso 注释

JAXB (JSR-222) 实现无法使用 Java 反射来确定所有可能的子类。作为解决方法,您可以使用 @XmlSeeAlso 注释来注释超类,该注释为 JAXB 提供对子类的引用。

@XmlSeeAlso({Sub.class})
public class SuperJAXBClass {
}

选项 #2 - 创建 JAXBContext 时传递子类

如果您在创建 JAXBContext 时包含子类,那么 JAXB 实现将会意识到它。当子类传入超类的元数据时,也会创建超类。

JAXBContext.newInstance(Sub.class);

You could do one of the following:

Option #1 - @XmlSeeAlso Annotation

JAXB (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.

@XmlSeeAlso({Sub.class})
public class SuperJAXBClass {
}

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.

JAXBContext.newInstance(Sub.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文