Friday, July 31, 2015

Snippet :: HOWTO Make JAXB Generate Joda-Time Classes

The key is using a bindings XML file with a javaType element.

pom.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated-zuora</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                   <wsdl>
                                      ${basedir}/src/main/resources/xml-schemas/zuora/zuora.a.69.0.wsdl
                                   </wsdl>
                                   <wsdlLocation>classpath:/xml-schemas/zuora/zuora.a.69.0.wsdl</wsdlLocation>
                                   <bindingFiles>
                                      <bindingFile>${basedir}/src/main/resources/xml-schemas/zuora/bindings.xml</bindingFile>
                                  </bindingFiles>
                                </wsdlOption>
                            </wsdlOptions>
                            <!-- generate toString()'s too -->
                            <extraarg>-xjc-Xts</extraarg>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

bindings.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.1" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- 
        so jaxb doesn't create JAXBElement<..>'s everything
         http://stackoverflow.com/a/4583912/423943
    -->
    <jaxb:globalBindings generateElementProperty="false"> 
        <!-- use JODA-Time DateTime for parsing xs:date -->
        <jaxb:javaType name="org.joda.time.LocalDate" xmlType="xs:date" parseMethod="org.joda.time.LocalDate.parse"/>
        <!-- use JODA-Time DateTime for parsing xs:dateTime -->
        <jaxb:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime" parseMethod="org.joda.time.DateTime.parse"/>
    </jaxb:globalBindings>
</jaxb:bindings> 

Monday, April 13, 2015