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-plugin
with ajaxb-bindings
file.First I created a
odt.xsd
file:Then I created a
jaxb-bindings.xjb
file, that defines the type of thedate
field, and also the class that converts from and to it:This file references the
xsd.test.OffsetDateTimeAdapter
class and the respective methods to convert theOffsetDateTime
from and to aString
, so I also created it:Then, in
pom.xml
I've added the configuration for the plugin:With this, I've just built the project with
mvn clean package
and the jar created contains the generated files in thexsd.test
package. TheTeste
class contains thedate
field as aOffsetDateTime
:With this, the
date
field is mapped to aOffsetDateTime
, using the autogeneratedAdapter1
(which internally uses thexsd.test.OffsetDateTimeAdapter
class created above). Example of parsing the date from a xml:And when marshalling the date to a xml, the
OffsetDateTime
is directly converted to aString
such 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/java
directory, at thexsd.test
package.