I have a web sercice method that recieves an object. One of the attributes is “interval” which is an integer.
I would like to make this atribute required but without providing any default value – I want the user to be required to explicitly set a value.
If I use int interval – the attribute is exposed as int and if the user does not explicitly set the attribute, a zero (Java default for primitive int) will be sent.
If I use Integer interval – the attribute is exposed as Integer and is declared optional in the WSDL so the user can’t see it is required before sending the request.
If I use Integer interval with @XmlElement(required = true) or @XmlElement(nillable = false) – the attribute is exposed as int.
The attribute can have any integer – negative, zero and positive so I can’t use a default value to indicate that the attribute was not explicitly set.
I can use BigInteger interval with @XmlElement(required = true) but than we are missing the advantages of using the core type Integer.
I would like to expose the attribute as Integer so I will get null if the user did not set the attribute and at the same time I would like the WSDL to expose that the attribute is required so users will know it is required simply by looking at the WSDL.
With
@XmlElement(required = true)the WSDL was correct but the problem was that when using org.apache.cxf.tools.wsdlto.WSDLToJava tool on my WSDL the client was generatedwith
int intervalinstead ofInteger interval.The solution is to pass the “-b” option to the wsdl2java tool with a path to a jaxb bindings file that maps xsd:int to
java.lang.Integer:Using
@XmlElement(required = true)and the “-b ” option, kept the element required (minOccurs=”1″) and exposed it as an Integer to the Java client.More info: http://cxf.547215.n5.nabble.com/How-to-declare-an-attribute-required-and-non-primitive-td4815370.html