Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how replace XmlGregorianCalendar by Date?

Tags:

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> 
like image 763
hunter99 Avatar asked Jun 19 '12 21:06

hunter99


People also ask

How do you convert GregorianCalendar to XMLGregorianCalendar?

datatype. DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

What is XMLGregorianCalendar format?

The Java XMLGregorianCalendar class, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes and is required to use the XML format.

What is the use of XMLGregorianCalendar?

2. XMLGregorianCalendar. The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.


2 Answers

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?

like image 138
Tomasz Nurkiewicz Avatar answered Sep 19 '22 18:09

Tomasz Nurkiewicz


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> 
like image 34
urOutsourced Avatar answered Sep 18 '22 18:09

urOutsourced