Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 443
Blessed Geek Avatar asked Dec 20 '16 15:12

Blessed Geek


People also ask

What is the difference between integer and BigInteger in Java?

BigInteger represents immutable arbitrary-precision integers. It is similar to the primitive integer types but allows arbitrary large values. It is used when integers involved are larger than the limit of long type. For example, the factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000.

What is XJB file in JAXB?

xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.


1 Answers

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;
}
like image 156
Jeroen van Montfort Avatar answered Sep 20 '22 13:09

Jeroen van Montfort