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

1

7

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;
Partiality answered 29/4, 2013 at 13:45 Comment(0)
A
7

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.

Acclivity answered 18/12, 2013 at 14:41 Comment(4)
The problem with int is that it does not support being null, Integer does.Outdate
All true, but my question was not about the target java type, but about the generated @XmlElement annotation. Also if I could change the XSD to use xs:int (which I can't, that's why I'm using an adapter), I suspect JAXB would generate an int OR an Integer, depending of the nullability of the field.Partiality
Specifying type="int" yields an int, specifying minOccurs=0 with type="int"yields an Integer.Interrogate
@EricDuminil I found I had to use nillable="true" rather than minOccurs=0Smoko

© 2022 - 2024 — McMap. All rights reserved.