JAXB javaType customization on xs:integer produces @XmlElement with "type=String.class"

Thomas Gillet picture Thomas Gillet · Apr 29, 2013 · Viewed 8.3k times · Source

When generating Java beans from a XSD with XJC, I need to map xs:integer to Integer rather than BigInteger. I added a javaType tag to my JAXB customization file (as said in many answers from this site), and it worked fine.

But in the generated code I noticed that the @XmlElement tag now has a type=String.class parameter.

So now I wonder, why String?
Is it because the parse and print methods are converting from/to string objects?

I tried with xjc:javaType instead of jaxb:javaType, allowing me to replace the generated Adapter1<String, Integer> with a custom MyAdapter<BigInteger, Integer>, but exactly the same thing happened.

If this is normal XJC behavior, is it possible to tweak it to generate code without this parameter, or with another value than String?

Note that everything is working fine, but I would like to understand.
Also I'm using Enunciate to document my API and it seems to be confused by this type thing (but this is probably a bug in Enunciate).


I'm using JAXB RI 2.2.6, and here are some pieces of code, to illustrate my question:

bindings.xjb

<jaxb:bindings version="2.0" ...>
    <jaxb:globalBindings>
        <jaxb:javaType
                name="java.lang.Integer"
                xmlType="xs:integer"
                parseMethod="..."
                printMethod="..." />
        </jaxb:globalBindings>
</jaxb:bindings>

Field definition in the XSD

<xs:complexType name="MyType">
    <xs:sequence>
        <xs:element name="myField" type="xs:integer" />
    </xs:sequence>
</xs:complexType>

Generated Java field

@XmlElement(namespace = "...", required = true, type = String.class)
@XmlJavaTypeAdapter(Adapter1.class)
@XmlSchemaType(name = "integer")
protected Integer myField;

Answer

Jeroen van Schelven picture Jeroen van Schelven · Dec 18, 2013

I know this is an old question, but for the people still looking for an answer: using type xs:int instead of xs:integer will create a normal java int instead of the Biginteger.