Reputation: 9406
We are using jaxb2 to generate Java code from xml schema definitions for some external data. The project is quite old and used the maven-jaxb-plugin 1.1.1. We want to update to jaxb2-maven-plugin which will also use jaxb2, but we found some differences in the generated code. Specifically, we have attributes in the form of
<xsd:attribute name="num" type="xsd:int" use="optional">
which are mapped to Integer
fields in the generated code.
@XmlAttribute(name = "num")
protected Integer num;
However, jaxb2 generates getters and setters with primitive type instead of nullable types:
public int getNum() {
return num;
}
public void setNum(int value) {
this.num = value;
}
public boolean isSetNum() {
return (this.num!= null);
}
public void unsetNum() {
this.num = null;
}
However, our current code assumes that getNum
returns a nullable boxed type and also tests this in unit tests which fail with a null pointer exception.
Is there a way to generate getters/setters with nullable types for optional attributes? The xsd files are provided from an external vendor so I would prefer to not modify them. We do not set optionalProperty
in <globalBindings>
, so the value is the default wrapper
.
Upvotes: 1
Views: 999
Reputation: 9406
I think I have a solution to my own question. In addition to optionalProperty
in globalBindings
, there is the option generateIsSetMethod
which controls if methods like isSetNum
shall be generated or not. If this is enabled, primitive types such as int
will be used instead of Integer
.
Upvotes: 1