Question

I generate my proxy class via wsdl2java goal in maven pom.

Sample xsd:

<xs:complexType name="address">
    <xs:sequence>
        <xs:element name="street" type="xs:string" />
        <xs:element name="homeNo" type="xs:int" />
    </xs:sequence>
</xs:complexType>

Generated class has homeNo property with int (primitive type). I would like to "big Integer" Wrapper type. How to force it? One way is add nillable="true" but it is terrible and it not looks well in schema.

Was it helpful?

Solution

You can customize the translation to and from Java types with a bindings file. For example, if you wanted to have all xsd:int elements bound to BigInteger, create a file bindings.xjb with the content

<jxb:bindings version="1.0"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:globalBindings>
        <!-- use BigInteger instead of int-->
        <jxb:javaType name="java.math.BigInteger" xmlType="xs:int"/>
    </jxb:globalBindings>  
</jxb:bindings>

When you call wsdl2java, use the -b option to specify the bindings file

wsdl2java -b bindings.xjb foo.wsdl

This will cause XJC to generate an adapter class, probably named something like

org.w3._2001.xmlschema.Adapter1

Your generated objects will make use of this class with the @XmlJavaTypeAdapter anotation.

You can also provide your own adapter methods using the parseMethod and printMethod attributes of <jxb:javaType />.

The Customizing JAXB Bindings section of the Java Web Services Tutorial provides more detail on what you can do with <jxb:javaType />.

OTHER TIPS

nilable=true or minOccurs="0" is the way to go, see XJC Generating Integer Instead of int

I prefer this solution compared to the globalBindings because of following reasons:

  1. globalBindings is a global switch that has consequences to all mappings. This may be preferable in some cases but it is rarely the case when you are working with Integers.
  2. nilable=true or minOccurs="0" are adding semantic to the mapping. It says that the values are nullable. If these attributes are missing one must assume that this is not the case. That said, you only should use Integer instead of int if the value is nullable.
  3. Using Integer instead of int is disastrous if you want to map a null Java value to the XML equivalent: What should be the outcome in the XML if in Java the value is null but this is not marked with the appropriate attributes in the XSD? This may generate invalid XML data.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top