Search code examples
javabindingjaxbxjc

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


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

  • using xs:int instead of xs:integer.
  • using primitive int instead of Integer.
  • why do you need this.
  • what are you gonna do if etc.

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;
    }