如何使用Apache CXF表示没有时区的日期?

我有一个WSDL,它指定一个元素的类型为xs:date。

当我使用Apache CXF生成Java类时,它将变量呈现为javax.xml.datatype.XMLGregorianCalendar(到目前为止一切都很好)。

当CXF呈现包含此文档的XML文档时,它将以此forms呈现它(其中-06:00表示时区):

2000-01-18-06:00

如何配置CXF不渲染时区?

默认情况下,wsdl的xsd:date被映射到XMLGregorianCalendar 。 如果这不是您想要的,那么如果您使用CXF的wsdl to java工具,那么您可以提供一个绑定文件来覆盖此默认映射:

        

您可以参考http://cxf.apache.org/docs/wsdl-to-java.html “如何将xsd:dateTime映射到java.util.Date?”部分。 更多细节。

 GregorianCalendar gcal = new GregorianCalendar(); start = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal); start.setTimezone(DatatypeConstants.FIELD_UNDEFINED); 

不要问我为什么在理智的逻辑中 – 当将XMLgregorianCalendar编组为xs:date时,它会保留时区。

我一直以为 – 时区可能更适用于xs:dateTime,但我所知道的……关于类型。

对我来说,对于xs:date类型,默认情况下没有时区是有意义的,这是编组逻辑中的一个问题。

要完成Filip的回答(感谢他!),也许它会帮助你们中的一些人……

我必须使用注释@XmlJavaTypeAdapter在关注字段日期声明一个新的XmlAdapter

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

适配器

 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消息日期格式

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

之后的SOAP消息日期格式

2017-04-18T00:00:00

我从2012年开始发表上述评论,现在我觉得有必要添加一个答案。 我正在对Web服务进行一些更改,遗憾的是,必须继续在Java 6上运行。我被要求在XML响应中禁止所有日期和时间字段的时区/偏移部分。 我使用JAXB绑定文件和3对适用于日期,时间和日期时间XML类型的适配器方法。 请注意,我的解决方案使用JodaTime库。

这是我的JAXB 2.1绑定文件:

         

这是我的Java 6兼容实用程序类和适配器方法:

 package com.jimtough.jaxb; import java.util.Date; import javax.xml.bind.DatatypeConverter; import org.joda.time.DateTime; import org.joda.time.LocalTime; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; /** * My bizarrely named 'condapter' is a blend of the Java {@code DatatypeConverter} * and the Apache CXF {@code DataTypeAdapter} that provides Jodatime {@code DateTime} * support instead of {@code java.util.Date}. * * @author jtough */ public class DataTypeCondapter { private DataTypeCondapter() {} // Jim Tough - 2017-02-22 // JodaTime formatters claim to be threadsafe private static final DateTimeFormatter DTF_DATE = ISODateTimeFormat.date(); private static final DateTimeFormatter DTF_DATETIME = ISODateTimeFormat.dateHourMinuteSecondMillis(); private static final DateTimeFormatter DTF_TIME = ISODateTimeFormat.hourMinuteSecondMillis(); public static DateTime parseDate(String s) { if (s == null) { return null; } Date date = DatatypeConverter.parseDate(s).getTime(); return new DateTime(date); } public static String printDate(DateTime dt) { if (dt == null) { return null; } return DTF_DATE.print(dt); } public static LocalTime parseTime(String s) { if (s == null) { return null; } Date date = DatatypeConverter.parseTime(s).getTime(); DateTime dt = new DateTime(date); return dt.toLocalTime(); } public static String printTime(LocalTime lt) { if (lt == null) { return null; } return DTF_TIME.print(lt); } public static DateTime parseDateTime(String s) { if (s == null) { return null; } Date date = DatatypeConverter.parseDateTime(s).getTime(); return new DateTime(date); } public static String printDateTime(DateTime dt) { if (dt == null) { return null; } return DTF_DATETIME.print(dt); } }