JAXB classImpl 绑定(使用扩展生成的 impl 的特定 impl)但 getter 返回超类型

发布于 11-15 12:25 字数 1005 浏览 3 评论 0原文

包装一些生成的类, 我使用 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 技术交流群。

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

发布评论

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

评论(2

维持三分热2024-11-22 12:25:58

您已定义类型可以由BWrapper实现。您必须明确指出 listB 的元素应该引用 BWrapper。

我不知道如何在架构中设置此内联,因此我不得不使用外部 .xjb 文件。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <!-- bindings in the scope of the schema -->
    <jaxb:bindings schemaLocation="./Test.xsd" node="/xs:schema">

        <!-- apply bindings in the scope of the complex type B. -->
        <jaxb:bindings node="//xs:complexType[@name='B']">
            <!-- the java BWrapper extends the B object created by XJC -->
            <jaxb:class implClass="com.foobar.BWrapper"/>
        </jaxb:bindings>

        <!-- specify bindings in the scope of the element 'listB' within -->
        <!-- the the complex type A -->
        <jaxb:bindings node="//xs:complexType[@name='A']//xs:element[@name='listB']">
            <!-- the element should reference the BWrapper cLass -->
            <jaxb:class ref="com.foobar.BWrapper"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

这将生成:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
    "listB",
    "singleB"
})
public class A {
    protected List<com.foobar.BWrapper> listB;
    @XmlElement(required = true, type = com.foobar.BWrapper.class)
    protected com.foobar.BWrapper singleB;

并且 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.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <!-- bindings in the scope of the schema -->
    <jaxb:bindings schemaLocation="./Test.xsd" node="/xs:schema">

        <!-- apply bindings in the scope of the complex type B. -->
        <jaxb:bindings node="//xs:complexType[@name='B']">
            <!-- the java BWrapper extends the B object created by XJC -->
            <jaxb:class implClass="com.foobar.BWrapper"/>
        </jaxb:bindings>

        <!-- specify bindings in the scope of the element 'listB' within -->
        <!-- the the complex type A -->
        <jaxb:bindings node="//xs:complexType[@name='A']//xs:element[@name='listB']">
            <!-- the element should reference the BWrapper cLass -->
            <jaxb:class ref="com.foobar.BWrapper"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

This will generate:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
    "listB",
    "singleB"
})
public class A {
    protected List<com.foobar.BWrapper> listB;
    @XmlElement(required = true, type = com.foobar.BWrapper.class)
    protected com.foobar.BWrapper singleB;

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.

等风来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;
}

示例请参见:

The type property on the @XmlElement is correct way to configure a JAXB implementation (Metro, MOXy, JaxMe, etc) for this use case. Do you still see the issue if you add an XmlAccessorType annotation to your class?

@XmlAccessorType(XmlAccessType.FIELD)
public class A {
    @XmlElement(type = BWrapper.class)
    protected List<B> listB;
    @XmlElement(required = true, type = BWrapper.class)
    protected BWrapper singleB;
}

For an example see:

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