JAXB binding to classImpl (to use a specific impl that extends the generated imp), but getter return super type

to wrap some generated classes, I use the classImpl binding, but the collections in the generated classes return the generated type instead of the type in classImpl, and I want, of course, a list of classImpl ...

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:

@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 by BWrapper, so why is listB a list of B instead of a list of BWrapper ???

in advance for your help!

+3
source share
2 answers

You have determined that a type can be implemented by BWrapper. You must explicitly say that the listB element must reference the 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;

getter listB BWrappers. , , .

+1

type @XmlElement - 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;
}

:

0

All Articles