Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing date of type XmlGregorianCalendar generated by wsdl to java.util.Date?

Tags:

java

date

wsdl

I am working on an application in which we give call to Third Party SAP system using files generated through wsdl using Spring webservices.

One of the file generated using wsdl through ws import have Date attribute of type "XMLGregorianCalendar" and in response we are getting null value for corresponding field .

I want to convert date from XmlGregorianCalendar to java.util.Date.

Have referred : how replace XmlGregorianCalendar by Date? but not able to provide appropriate xjb bindings through wsdl.

If anyone can suggest the conversion of Dates generated by wsdl ,it would be of great help..... Thanks In Advance ! Shuchi

like image 359
shuchi Avatar asked Nov 11 '16 13:11

shuchi


1 Answers

WSDL has no deal with xjb. xjb is for xjc compiler passed as -b parameter. i.e.

xjc -b <file>

documentation: Customizing JAXB Binding

if you use Maven plugins to generate your JAXB Java classes any of them have Binding configuration i.e.

<groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <configuration>
                    <defaultOptions>
                        <bindingFiles>
                            <bindingFile>${project.interfaces.basedir}Configuration/Bindings/common-binding.xjb</bindingFile>
                        </bindingFiles>

or

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/XMLSchema</schemaDirectory>
                <bindingDirectory>${basedir}/src/main/resources/Bindings</bindingDirectory>
            </configuration>

and so on...

xjb for it is very simple:

<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings>
    <jaxb:serializable uid="1" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:dateTime"
        parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
        printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:date"
        parseMethod="javax.xml.bind.DatatypeConverter.parseDate" printMethod="javax.xml.bind.DatatypeConverter.printDate" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:time"
        parseMethod="javax.xml.bind.DatatypeConverter.parseTime" printMethod="javax.xml.bind.DatatypeConverter.printTime" />            
</jaxb:globalBindings>

as you can see it defines conversions from xsd:dateTime, xsd:date and xsd:time types to java.util.Calendar.

I do not recommend to use java.util.Date. There are many troubles with Date handling (especially with different timeZones). It is better to use java.util.Calendar. Calendar is much easier to handle and default converter implementation is there in JDK:

javax.xml.bind.DatatypeConverter

But, if you still want to use java.Util.Date you need to have your own small converter with two static methods "parse" and "print" and then set it in xjb. i.e.

public class MyDateConverter {
    public static java.util.Date parse(String xmlDateTime) {
        return javax.xml.bind.DatatypeConverter.parseDateTime(xmlDateTime).getTime();
    }

    public static String print(Date javaDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(javaDate.getTime());
        return javax.xml.bind.DatatypeConverter.printDateTime(calendar);
    }
}

your conversions in xjb will look like:

<jaxb:javaType name="java.util.Date" xmlType="xsd:dateTime"
    parseMethod="MyDatatypeConverter.parse"
    printMethod="MyDatatypeConverter.print" />
like image 125
Vadim Avatar answered Oct 06 '22 06:10

Vadim