Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

web services - how replace XmlGregorianCalendar by Date?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...