how replace XmlGregorianCalendar by Date?
Asked Answered
K

2

22

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>
Kazim answered 19/6, 2012 at 21:19 Comment(0)
I
19

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?

Isologous answered 19/6, 2012 at 21:36 Comment(2)
Hey Tomasz I tried your solution and I was actually generating the sources using maven plugin. It did generate all the sources necessary and it did generate Date rather than XMLGregorianCalendar and it also annotated with @XmlJavaTypeAdapter(Adapter2 .class) @XmlSchemaType(name = "date") protected Date closedDate; but my problem is it couldnt find the XsdDateTimeConverter in the adapter classes and there was a compilation error. Can you please help me on this.Paragraph
Solved it by specifying the full package name before the class name.Believe
E
1

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>
Envelopment answered 27/8, 2016 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.