How do I represent dates without the timezone using Apache CXF?

后端 未结 4 2004
暗喜
暗喜 2021-02-04 05:52

I have a WSDL that specifies an element\'s type to be xs:date.

When I use Apache CXF to generate the Java classes, it renders the variable as an javax.xml.datatype.XMLGr

4条回答
  •  盖世英雄少女心
    2021-02-04 06:30

    To complete Filip answer (thanks to him!), maybe it will help some of you ...

    I had to declare a new XmlAdapter on the concern field date with the annotation @XmlJavaTypeAdapter

    public class YourDTO {
       // ... 
       @XmlElement
       @XmlSchemaType(name = "dateTime")
       @XmlJavaTypeAdapter(type = XMLGregorianCalendar.class, value = XmlDateAdapter.class)
       public Date yourDate;
       // ...
    }
    

    The adapter

    public class XmlDateAdapter extends XmlAdapter {
    
    @Override
    public XMLGregorianCalendar marshal(Date date) throws Exception {
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(date);
        XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
        xmlDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
        return xmlDate;
    }
    // ...
    

    SOAP message date format before

    2017-04-18T00:00:00+02:00

    SOAP message date format after

    2017-04-18T00:00:00

提交回复
热议问题