I’m using JAXB(xjc version 2.2.4-2) to generate Java classes from a XML Schema. The XML types that map to a Java primitive datatype don’t add:
@XmlElement(required = true)
For example when using:
<element name="userId" type="long"/>
<element name="userName" type="string"/>
will result in:
//no annotation added here
protected long userId;
@XmlElement(required = true)
protected String userName;
Does anyone have an explanation why this happens?
Does any of this have to do with options that you can set with xjc?
You don’t need an annotation to show that a property of Java type
longis required as this is implicit from the fact that primitive values can’t be null. A non-nillable required element of typexs:longmaps to Javalong, an optional or nillable one maps tojava.lang.Long(which permitsnull, representing absent orxsi:nilas appropriate).An element that is both optional and nillable (odd, but allowed by XML Schema) would map to a
JAXBElement<Long>to distinguish between absent (anullJAXBElement) and nil (a non-nullJAXBElementwhoseisNil()returns true).