My model object look something like this:
@XmlRootElement(name = "appConfig")
@XmlType(propOrder = {})
public class Config implements Serializable {
...
private int advancedFooBar;
...
@XmlElement(name = "advancedfoobar")
public int getAdvancedFooBar() {
return advancedFooBar;
}
public void setAdvancedFooBar(int advancedFooBar) {
this.advancedFooBar = advancedFooBar ;
}
When I generate a schema against this class w/JXC I end up with the following for the above property:
<xs:element name="advancedfoobar" type="xs:int"/>
I’d like this to be an optional element so I tried changing
@XmlElement(name = "advancedfoobar")
to
@XmlElement(name = "advancedfoobar", required=false)
However, that did not result any change to the generated schema. What do I need to do so that the “advancedfoobar” element will be defined as optional in the generated schema?
I am using JDK 1.7.0_U3 on Windows 7 Ultimate x64.
Thanks.
-Noah
WHAT THE JAVADOCS SAY
According to the Javadoc for
@XmlElement(see: http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElement.html#required%28%29)The schema definition for
advancedfoobarshould beminOccurs=0for the following mappings:and
BUG IN MOXy AND REFERENCE IMPLEMENTATION
There appears to be a bug in both EclipseLink JAXB (MOXy) and the JAXB reference implementation regarding primitives and optional elements. I have opened the following bug against MOXy.
WORKAROUND
You can make the property of type
Integerinstead ofint. Or better yet just set thetypeproperty on the@XmlElementannotation to beInteger. Any type capable of holding anullvalue will be optional by default.Config
Demo
Output