We have a xsd with xs:dateTime fields. This is our in-house internal API, and we can guarantee that the offset data is always included, so that it's ISO-8601 compatible. For example:
2016-01-01T00:00:00.000+01:00
Currently, the jaxb2 plugin maps xs:dateTime to a field of type XMLGregorianCalendar. How to configure the plugin, so that it uses OffsetDateTime instead?
I don't care whether the solution is for maven-jaxb2-plugin, jaxb2-maven-plugin or cxf-codegen-plugin, we'll use whichever works.
You can use the
jaxb2-maven-pluginwith ajaxb-bindingsfile.First I created a
odt.xsdfile:Then I created a
jaxb-bindings.xjbfile, that defines the type of thedatefield, and also the class that converts from and to it:This file references the
xsd.test.OffsetDateTimeAdapterclass and the respective methods to convert theOffsetDateTimefrom and to aString, so I also created it:Then, in
pom.xmlI've added the configuration for the plugin:With this, I've just built the project with
mvn clean packageand the jar created contains the generated files in thexsd.testpackage. TheTesteclass contains thedatefield as aOffsetDateTime:With this, the
datefield is mapped to aOffsetDateTime, using the autogeneratedAdapter1(which internally uses thexsd.test.OffsetDateTimeAdapterclass created above). Example of parsing the date from a xml:And when marshalling the date to a xml, the
OffsetDateTimeis directly converted to aStringsuch as2016-01-01T00:00+01:00.Another way is to use the command line
xjc, which comes with the JDK:This generates the classes in
src/main/javadirectory, at thexsd.testpackage.