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

前端 未结 2 1877
忘了有多久
忘了有多久 2021-01-12 15:00

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

相关标签:
2条回答
  • 2021-01-12 15:33

    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.

    0 讨论(0)
  • 2021-01-12 15:38

    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>
    
    0 讨论(0)
提交回复
热议问题