users@jaxb.java.net

Re: force to use primitive types

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 27 May 2011 12:01:46 +0200

From the JAXB sepcification:

<globalBindings
   ...
    [ optionalProperty = "wrapper" | "primitive" | "isSet" ]
   ...
</globalBindings>

optionalProperty controls how a JAXB property with a primitive
base type that represents an optional non-nillable element/
attribute is bound. If the attribute has the value "wrapper", then
the base type of the JAXB property is the wrapper class for the
primitive type. A user can indicate that this optional property
is not set by calling the setter with “null” value. If the
attribute’s value is "primitive", it binds as it did in JAXB 1.0.
If the attribute’s value is “isSet”, it binds the optional
property using the primitive base type and also the isSet/unset
methods are generated for the optional property. The attribute’s
default value is “wrapper”.

-W

On 27 May 2011 11:19, aka.fe2s <aka.fe2s_at_gmail.com> wrote:

> Hello,
>
> I'm migrating application from JAXB 1 to JAXB 2. The problem I faced
> is that previously JAXB 1 generated primitives types (int, boolean,
> ..) and isSetXXX() methods everywhere, even though the element was
> optional (minOccurs="0"). If the element was absent in XML, it set the
> property to default value (0 for int, false for boolean, ...)
>
> The problem is that JAXB 2 generates wrappers (Integer, Boolean) in
> such cases. It leads to NPE in my code.
> I tried to set
> <jxb:globalBindings
> generateIsSetMethod="true" />
>
> and/or
>
> <jxb:javaType name="int" xmlType="xs:int"
> parseMethod="javax.xml.bind.DatatypeConverter.parseInt"
> printMethod="javax.xml.bind.DatatypeConverter.printInt" />
>
> but it doesn't help.
>
> Is there a way to force JAXB 2 behave like JAXB 1 ?
>
> Thanks, Oleksiy.
>