Java Reference
In-Depth Information
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"firstName",
"lastName"
})
@XmlRootElement(name = "author")
public class Author {
@XmlElement(required = true)
protected String firstName;
@XmlElement(required = true)
protected String lastName;
//... getters and setter methods ommitted.
The getter and setter methods were left out of this output listing because they do not get any
annotations attached to them. Notice that the Russian Doll design pattern is employed here,
which causes the class to get the @XMLRootElement annotation. Had you defined the com-
plexType outside of the element and left everything else the same, JAXB would have mul-
tiple possible roots to choose from, and leave off the annotation. Put another way, JAXB could
not statically guarantee that your complexType would not be used in other schemas that might
be compiled separately, at another time, which would cause such a collision.
It is illegal to attempt to marshal multiple nested objects annotated with @XMLRootElement .
Let's take a quick look at some of the annotations used here.
XMLAccessorType
This annotation indicates which of four possible values will be used to marshal child elements:
PUBLIC_MEMBER
Indicates that the marshaler should marshal every Java bean property (public getter and
setter pair).
FIELD
Tells the marshaler to marshal every nonstatic, nontransient field in the class. To prevent a
field from being marshaled, annotate it with XMLTransient .
PROPERTY
Tells the marshaler to marshal every getter/setter pair, regardless of their access visibility.
Search WWH ::




Custom Search