Convert date type to string in jaxb using bindings

2.4k views Asked by At

I'm getting the error message:

Could not convert xsd:date to java.lang.String type

I'm using a binding.xml file with CXF XJC plugin

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Date" type="xsd:date"/>
    <xsd:element name="Audit">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Creation">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="Date"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

binding.xml

<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings>
        <jaxb:globalBindings>
            <jaxb:javaType name="java.lang.String" xmlType="xsd:date"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings> 

cxf xjc plugin:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>3.0.3</version>
        <executions>
            <execution>
                <id>generate-resources</id>
                <phase>generate-resources</phase>
                <configuration>
                    <defaultOptions>
                        <bindingFiles>
                            <bindingFile>${basedir}/src/main/resources/META-INF/binding.xml</bindingFile>
                        </bindingFiles>
                        <noAddressBinding>true</noAddressBinding>
                    </defaultOptions>
                    <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                    <xsdOptions>
                        <xsdOption>
                            <xsd>${basedir}/src/main/resources/xsd/Misc.xsd</xsd>
                            <packagename>com.mycomp.ext.schema</packagename>
                            <extension>true</extension>
                        </xsdOption>
                    </xsdOptions>
                </configuration>
                <goals>
                    <goal>xsdtojava</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Generated code contains XMLGregorianCalendar instead of String

    @XmlElement(name = "Date", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar date;

Any suggestions please?

2

There are 2 answers

1
vinothM On

If you change the xml type to xsd:date xsd:string it should be working

0
vinothM On

Sorry I mean if you need to access it as string field in java you should define that schema with xsd:string type instead of the xsd:date type.

Option one: If you want to use the field as date in java you can format it in java later.
Option two: If you want to make schema to take care of data type you should define as xsd:date and use the XMLGregorianCalendar which can be converted to Calendar or to normal Date java object later in java. This way the date format validation will be done by schema itself.