javabindingjaxbxjc

JAXB binding for xs:integer to Java Integer instead of BigInteger


The following discussions should be avoided. What this question is NOT:

QUESTION:

I would like an example of a jaxb binding declaration to override the default mapping of xs:integer to Java BigInteger, so that xjc would produce Java Integer instead.

e.g.

<xs:attribute name="id" type="xs:integer"/>

should produce

@XmlAttribute(name = "id")
Integer id;

and not

@XmlAttribute(name = "id")
BigInteger id;

Solution

  • You could add the following to your bindings file:

    <globalBindings>
        <javaType xmlType="xs:integer" name="java.lang.Integer"/> 
    </globalBindings>
    

    This will produce, for example:

    @XmlAttribute(name = "id")
    @XmlJavaTypeAdapter(Adapter1 .class)
    protected Integer id;
    
    public int getId() {
        if (id == null) {
            return new Adapter1().unmarshal("0");
        } else {
            return id;
        }
    }
    
    public void setId(Integer value) {
        this.id = value;
    }