Why is the @XmlRootElement annotation marked for
Spring requires a root element when sorting an object in XML. JAXB provides two mechanisms for this:
- @XmlRootElement annotations
- Wrap around the root object in a JAXBElement instance.
Because the object is not wrapped in a JAXBElement, Spring provides another condition.
How to create an @XmlRootElement element
JAXB will generate the @XmlRootElement annotation for all global elements in the XML schema. The following will result in @XmlElement:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="foo"> <xsd:complexType> ... </xsd:complextType> </xsd:element> </xsd:schema>
When @XmlRootElement is not generated
@XmlRootElement annotations will not be generated for global types.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="foo" type="foo"/> <xsd:complexType name="foo"> ... </xsd:complexType> </xsd:schema>
Instead, the global element (s) associated with the global types is captured in the ObjectFactory class (annotated with @XmlRegistry) as @XmlElementDecl annotations. These annotations
package generated; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.namespace.QName; @XmlRegistry public class ObjectFactory { private final static QName _Foo_QNAME = new QName("", "foo"); public Foo createFoo() { return new Foo(); } @XmlElementDecl(namespace = "", name = "foo") public JAXBElement<Foo> createFoo(Foo value) { return new JAXBElement<Foo>(_Foo_QNAME, Foo.class, null, value); } }
The @XmlElementDecl annotation provides similar information as @XmlRootElement and can be used for operations without a marshal. JAX-RS implementations probably don't use @XmlElementDecl, however, since marshal operations require the object to be wrapped in a JAXBElement object to provide a root name / namespace.