Reputation: 103
I have the following JAXB .xsd schema file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1" xmlns="some.namespace.com"
targetNamespace="some.namespace.com">
<xs:element name="outerModel">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="OuterModelDto" />
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="innerModel" type="innerModelDto" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="innerModelDto">
<xs:sequence>
<xs:element name="fullname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
This will create an OuterModelDto
and an InnerModelDto
class. The OuterModelDto
is annotated with @XmlRootElement
, but the InnerModelDto
is not. How can i ensure that InnerModelDto
is also annotated with @XmlRootElement
?
One solution was to wrap the innerModelDto complexType
in an element, which meant that i did end up with @XmlRootElement
on both Dto
s, but during serialization of OuterModelDto
, the namespace information was serialized, which i do not want.
Upvotes: 0
Views: 710
Reputation: 43671
You can use my jaxb2-annotate-plugin to annotate innerModelDto
with @XmlRootElement
. See (and upvote) this answer by Xstian:
The binding would be like:
<bindings node="xs:complexType[@type='innerModelDto']">
<annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement(name="innerModel")</annox:annotateClass>
</bindings>
Disclaimer: I am the author of the jaxb2-annotate-plugin.
Upvotes: 0
Reputation: 1892
You can separate the complexType
and element
definitions in the schema. Each element
will result in a @XmlRootElement
annotation.
Make sure you declare some namespace prefix in your schema to be able to refer to the types, see the xmlns:tns
here:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
targetNamespace="some.namespace.com"
xmlns:tns="some.namespace.com">
<xs:complexType name="OuterModelDto">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="innerModel" type="innerModelDto" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="InnerModelDto">
<xs:sequence>
<xs:element name="fullname" type="xs:string" />
<xs:element name="surname" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
<xs:element name="outerModel" type="tns:OuterModelDto" />
<xs:element name="innterModel" type="tns:InnerModelDto" />
</xs:schema>
Upvotes: 1