JAXB classImpl 绑定(使用扩展生成的 impl 的特定 impl)但 getter 返回超类型
包装一些生成的类, 我使用 classImpl 绑定,但生成的类中的集合返回生成的类型而不是 classImpl 中的类型,当然我想要一个 classImpl 列表...
我的 xsd:
<complexType name="A">
<xs:sequence>
<element name="listB" type="sbs:B" minOccurs="0" maxOccurs="unbounded"></element>
<element name="singleB" type="sbs:B" minOccurs="1" maxOccurs="1"></element>
</xs:sequence>
</complexType>
<complexType name="B">
<xs:annotation><xs:appinfo>
<jxb:class implClass="BWrapper" />
</xs:appinfo></xs:annotation>
</complexType>
生成的类是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
"listB",
"singleB"
})
public class A {
@XmlElement(type = BWrapper.class)
protected List<B> listB;
@XmlElement(required = true, type = BWrapper.class)
protected BWrapper singleB;
正如预期的 singleB 类型为 BWrapper,所以, 为什么listB是B的列表而不是BWrapper的列表???
预先感谢您的帮助!
to wrap some generated classes,
i use the classImpl binding but collections in generated classes return the generated type instead of the type in classImpl and i want a list of classImpl of course ...
my xsd:
<complexType name="A">
<xs:sequence>
<element name="listB" type="sbs:B" minOccurs="0" maxOccurs="unbounded"></element>
<element name="singleB" type="sbs:B" minOccurs="1" maxOccurs="1"></element>
</xs:sequence>
</complexType>
<complexType name="B">
<xs:annotation><xs:appinfo>
<jxb:class implClass="BWrapper" />
</xs:appinfo></xs:annotation>
</complexType>
generated classes are:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
"listB",
"singleB"
})
public class A {
@XmlElement(type = BWrapper.class)
protected List<B> listB;
@XmlElement(required = true, type = BWrapper.class)
protected BWrapper singleB;
as expected singleB is typed BWrapper, so,
why a listB is a list of B instead of a list of BWrapper ???
thanks in advance for your help !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
等风来2024-11-22 12:25:58
@XmlElement
上的 type
属性是为此用例配置 JAXB 实现(Metro、MOXy、JaxMe 等)的正确方法。如果您向类添加 XmlAccessorType
注释,您是否仍然会遇到此问题?
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
@XmlElement(type = BWrapper.class)
protected List<B> listB;
@XmlElement(required = true, type = BWrapper.class)
protected BWrapper singleB;
}
示例请参见:
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您已定义类型可以由BWrapper实现。您必须明确指出 listB 的元素应该引用 BWrapper。
我不知道如何在架构中设置此内联,因此我不得不使用外部 .xjb 文件。
这将生成:
并且 listB 的 getter 返回 BWrappers 列表。我不确定为什么单个项目和列表之间存在这种不一致,但至少这是有效的。
You have defined that the type can be implemented by BWrapper. You must explicitly say that the element listB should reference BWrapper.
I couldn't figure out how to set this inline in the schema, so I had to use an external .xjb file.
This will generate:
And the getter for listB returns a list of BWrappers. I'm not sure why there is this inconsistency between single items and lists, but at least this works.