22
votes

I have to expose an ejb service layer via jax-ws .

I have generated the web service using jax-ws and wsimport but I'm stopped by a strange things ; Date are being mapped to XmlGregorianCalendar . Is it possible to use classic java Date instead ? Can you show me the right way to proceed ?

Thanks . Edit: this the binding file i used : thanks , I modified slightly your xml and attached it with netbeans to the client's webservice and it worked . This the binding I used :

<jaxws:bindings  node="wsdl:definitions/wsdl:types/xsd:schema"
                 xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"

                                xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"

                                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

                                xmlns:xsd="http://www.w3.org/2001/XMLSchema" wsdlLocation="../wsdl/localhost_8080/web_test/Testor.wsdl" >


 <jaxb:globalBindings>
          <jaxb:javaType   name="java.util.Date"
        xmlType="xsd:dateTime"
        parseMethod="lol.XsdDateTimeConverter.unmarshal"
        printMethod="lol.XsdDateTimeConverter.marshalDateTime"        
          /><jaxb:javaType 
        name="java.util.Date"
        xmlType="xsd:date"
        parseMethod="lol.XsdDateTimeConverter.unmarshal"
        printMethod="lol.XsdDateTimeConverter.marshalDate"
        />
      </jaxb:globalBindings>


</jaxws:bindings>
2

2 Answers

19
votes

Not tested, but should work. First create such class:

import javax.xml.bind.DatatypeConverter;

public class XsdDateTimeConverter {

    public static Date unmarshal(String dateTime) {
        return DatatypeConverter.parseDate(dateTime).getTime();
    }

    public static String marshalDate(Date date) {
        final GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return DatatypeConverter.printDate(calendar);
    }

    public static String marshalDateTime(Date dateTime) {
        final GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(dateTime);
        return DatatypeConverter.printDateTime(calendar);
    }

}

Then add this to custom xjb file:

<javaType
        name="java.util.Date"
        xmlType="xs:dateTime"
        parseMethod="XsdDateTimeConverter.unmarshal"
        printMethod="XsdDateTimeConverter.marshalDateTime"
        />
<javaType
        name="java.util.Date"
        xmlType="xs:date"
        parseMethod="XsdDateTimeConverter.unmarshal"
        printMethod="XsdDateTimeConverter.marshalDate"
        />
</globalBindings>

Not tested, but should work. Based on my answer here: JAX-WS and Joda-Time?

1
votes

Thanks Tomasz. The above solution works.
But wsimport also adds its set of Adapters like Adapter1.java and Adapter2.java with its package org.w3._2001.xmlschema, which really doesnot match my own package structure.

I found a way to change this package name using another jaxb binding. Actually, I searched for this a lot and could not find this easily, so I am adding it here for anyone looking for the same.

Add the following binding in the wsimport using '-b binding.xml'. Note that wsimport can work with multiple binding files.

binding.xml content below:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  jaxb:version="2.0">
  <annotation><appinfo>
    <jaxb:schemaBindings>
      <jaxb:package name="com.abc.xyz.utils"/>
    </jaxb:schemaBindings>
  </appinfo></annotation>
</schema>