Search code examples
javamavenwsdlcxfwsdl2java

Apache CXF I don't want primitives


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.


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 />.