Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to generate java.util.Calendar from xsd - getting XmlGregorianCalendar

Tags:

wsdl

jaxb

xsd

cxf

Is it possible to generate entities with Claendar type fields from xsd files? I am trying both xs:date and xs:dateTime but still getting XMLGregarionCalendar. I am using cxf-codegen-plugin and jaxb bninding. Thanks. Paul.

like image 328
johnny-b-goode Avatar asked Jan 22 '14 15:01

johnny-b-goode


2 Answers

When you generate your objects you can use a JAXB binding file, as demonstrated in Example 7 of the cxf-codegen-plugin documentation. Depending on what type you want to use (Calendar, Date, etc.), you will need to specify an appropriate adapter. To use Calendar, JAXB provides the adapter javax.xml.bind.DatatypeConverter. To use it with dateTime, date, and time, the JAXB bindings file should be

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="2.1"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

    <jxb:globalBindings>
        <!-- use Calendar instead of XMLGregorianCalendar -->
        <jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
            printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>

        <jxb:javaType name="java.util.Calendar" xmlType="xs:date"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
            printMethod="javax.xml.bind.DatatypeConverter.printDate"/>

        <jxb:javaType name="java.util.Calendar" xmlType="xs:time"
            parseMethod="javax.xml.bind.DatatypeConverter.parseTime"
            printMethod="javax.xml.bind.DatatypeConverter.printTime"/>

    </jxb:globalBindings>        
</jxb:bindings>

If you want to use Date instead, CXF provides org.apache.cxf.xjc.runtime.DataTypeAdapter in cxf-xjc-runtime.

like image 190
Patrick Avatar answered Nov 15 '22 16:11

Patrick


Building on Patrick's answer, here is the XJC equivalent:

<jaxb:bindings version="2.1"
              xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

    <jaxb:globalBindings>
       <!-- use Calendar Date instead of XMLGregorianCalendar -->
       <jaxb:javaType name="java.util.Date" xmlType="xs:dateTime"
                 parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDateTime"
                 printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printDateTime"/>

       <jaxb:javaType name="java.util.Date" xmlType="xs:date"
                 parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDate"
                 printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printDate"/>

       <jaxb:javaType name="java.util.Date" xmlType="xs:time"
                 parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseTime"
                 printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printTime"/>

    </jaxb:globalBindings>         
</jaxb:bindings>
like image 25
ThomasRS Avatar answered Nov 15 '22 14:11

ThomasRS