JAXB2 type restriction not working?

佐手、 提交于 2019-12-05 19:20:33

Ok, I'll post this as an answer.

You run into this problem because of this type of the construct:

<xs:complexType name="MessageType" abstract="true">
    <xs:annotation>
        <xs:documentation>MessageType is an abstract type which is used by all of the messages, to allow inheritance of common features. Every message consists of a mandatory header, followed by optional payload (which may occur multiple times), and finally an optional footer section for conveying error, warning, and informational messages.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="Header" type="BaseHeaderType"/>
        <xs:any namespace="##targetNamespace" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="footer:Footer" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>   

...

<xs:complexType name="GenericDataType">
    <xs:annotation>
        <xs:documentation>GenericDataType defines the contents of a generic data message.</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:restriction base="MessageType">
            <xs:sequence>
                <xs:element name="Header" type="GenericDataHeaderType"/>
                <xs:element name="DataSet" type="data:DataSetType" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="footer:Footer" minOccurs="0"/>
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

One element is restricted to a more specific element/type (DataSet). Since all the properties are already defined on the super classe, XJC does not generate them on the "inheriting class"

To my knowledge, there is a number of issues reported on restrictions in JAXB RI. Derivation by restriction seems to be a hard concept for JAXB.

For instance, in this case - how do you think GenericDataType should look like? You basically need to override the header property to use a more specific type. Since XJC places annotations on fields rather than getters/setter, I wonder how you could override such a property. You can add another header property to handle your header, but override?

Try writing such a class - manually and use jaxb:class/@ref-binding. If you make it work, this would give an idea of what should be generated.

Next, if it is feasible via an XJC plugin or not.

The issue you report here is actually XJC's core business. If something don't work there then the "good" thing would be to report (or find an already reported issue) and fix the problem in XJC.

XJC plugins can do really a lot of things. You can completely restructure the model and customize the generation. With the appropriate effort, everything is possible.

But in this case this might be a risky endeavour. Writing advanced XJC plugins is not easy.

If you have a good concept on what exactly the plugin should do and how this particular issue could be fixed in the schema-derived classes, post your thoughts, maybe we'll be able to provide some guidance. Me personally, I've probably written more XJC plugins than anyone else.

I hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!