为什么 JAXB 有时会映射到 JAXBElement?
非官方指南中有一个占位符答案,其中包含一篇文章的链接(对我来说)似乎完全无关。
我使用 XJC 生成 JAXB 类,虽然其中大多数类按预期相互映射,但某些元素会映射到 JAXBElement
。这对于具有循环的图形来说是最烦人的,有时 Foo 元素的父节点是 JAXBElement
,它本身没有父属性,从而破坏了循环。
我可以想到各种解决方法,但如果有人可以向我解释这种行为,那就更好了。为什么 JAXB 有时会将
元素映射到 JAXBElement
而不是 Foo?
There is a placeholder answer over at the unofficial guide with a link to an article which (to me) seems quite unrelated.
I use XJC to generate my JAXB classes and while most of them map to each other as expected, some elements get mapped to JAXBElement<Foo>
. This is most annoying for graphs with cycles, where sometimes the parent node of a Foo element will be the JAXBElement<Foo>
, which doesn't itself have a parent property, breaking the cycle.
I can think of various workarounds, but it would be much nicer if someone could explain this behaviour to me. Why does JAXB sometimes map a <Foo>
element to JAXBElement<Foo>
instead of Foo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JAXBElement 用于在对象模型中没有足够信息的用例中保留元素名称/命名空间。最常见的情况是使用替换组:
使用替换组:
将生成:
不使用替换组:
如果删除替换组:
将生成以下类:
您可以解组时也会得到一个 JAXBElement,比较以下示例:
JAXBElement is used to preserve the element name/namespace in use cases where enough information is not present in the object model. The most common occurence is with substitution groups:
With Substitution Group:
Will generate:
Without Substitution Group:
If you remove the substitution group:
The following class will be generated:
You may also get a JAXBElement when you unmarshal, compare the following examples:
避免映射到 JAXBElement?
我已经完成了这对我有用的事情:
将此文件保存在 xml 文件中并作为绑定文件提供。
参考链接
使用eclipse生成JAXB类不要忘记给出绑定使用上述 xml 创建的文件并选中“允许供应商扩展”,如下所示:
通过更改,我能够摆脱生成的 xjc 类中的 JAXBElement,以将 xsd 转换为 JAXB 生成的类。
To avoid mapping to JAXBElement?
I have done this things it work for me :
Save this file in the xml file and supply as binding files.
Reference Link
Generate JAXB classes using eclipse dont forget to give binding files created with above xml and check "Allow Vendor extensions" as below:
With changes i am able Get rid of JAXBElement in classes generated xjc for convert xsd to JAXB generated classes.