My goal is to replace XMLGregorianCalendar by a different Java class (e.g. java.time.LocalDate).
I'm running wsdl2java as part of a maven pom.xml file:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>4.0.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>/path/to/the.wsdl</wsdl>
<bindingFiles>
<bindingFile>/path/to/binding.xml</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-verbose</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
The binding file looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings 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"
jaxb:version="2.1">
<jaxb:globalBindings>
<xjc:javaType name="java.time.LocalDate"
xmlType="xs:dateTime"
adapter="my.app.LocalDateAdapter" />
</jaxb:globalBindings>
</jaxb:bindings>
However, the generated Java file still uses XMLGregorianCalendar (which is the default class) for dateTime XML types:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SomeType", propOrder = {
"someDate"
})
public class SomeType {
@XmlElement(required = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar someDate;
...
}
I was expecting the generated Java code to use java.time.LocalDate (instead of XMLGregorianCalendar) for dateTime XML types.
I have seen so many different variations of the binding file but could not get one of them to work. The one binding file mentioned above is the most promising (but obviously wrong) version I came up with.
Although verbose logging is enabled (<extraarg>-verbose</extraarg>), none of the wsdl2java log messages indicate an issue in the binding file. Whatever I try, it feels like it is completely ignoring the binding file.
Any idea about what I'm doing wrong?
To make sure the generated Java code uses java.time.LocalDate instead of XMLGregorianCalendar for dateTime XML types, you need to configure the cxf-codegen-plugin to use the JAXB binding file during code generation. It seems like the binding file is not being picked up currently. You can modify the configuration of the plugin in your pom.xml as follows:
Make sure to replace /path/to/binding.xml with the actual path to your binding file.
With this configuration, the cxf-codegen-plugin should now use the specified binding file during code generation, and the generated Java classes will use java.time.LocalDate instead of XMLGregorianCalendar for dateTime XML types. Additionally, the true option enables the plugin to generate Java 8 Date/Time API classes in the generated code.
Remember to have the javax.xml.bind:jaxb-api dependency in your pom.xml to ensure that the Java 8 Date/Time API is used properly.