我有一个WSDL,它将元素的类型指定为xs:date。
当我使用Apache CXF生成Java类时,它将变量呈现为javax.xml.datatype.XMLGregorianCalendar(到目前为止都是好的)。
当CXF呈现包含此文档的XML文档时,它会以此形式呈现它(其中-06:00表示时区):
2000-01-18-06:00
如何配置CXF不渲染时区?
答案 0 :(得分:64)
GregorianCalendar gcal = new GregorianCalendar();
start = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
start.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
不要问我为什么在理智的逻辑中 - 当将XMLgregorianCalendar编组为xs:date时,它会保留时区。
我一直认为 - 时区可能更适用于xs:dateTime,但我所知道的......关于类型。
对我来说,默认情况下为xs:date类型设置时区是没有意义的,这是编组逻辑中的一个问题。
答案 1 :(得分:5)
要完成Filip的回答(感谢他!),也许它会帮助你们中的一些人......
我必须使用注释@XmlJavaTypeAdapter
在关注字段日期声明一个新的XmlAdapterpublic class YourDTO {
// ...
@XmlElement
@XmlSchemaType(name = "dateTime")
@XmlJavaTypeAdapter(type = XMLGregorianCalendar.class, value = XmlDateAdapter.class)
public Date yourDate;
// ...
}
适配器
public class XmlDateAdapter extends XmlAdapter<XMLGregorianCalendar, Date> {
@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
答案 2 :(得分:4)
默认情况下,wsdl的xsd:date
会映射到XMLGregorianCalendar
。如果这不是您想要的,那么如果您使用的是CXF的wsdl to java
工具,那么您可以提供一个绑定文件来覆盖此默认映射:
<jaxws:bindings wsdlLocation="YOUR_WSDL_LOCATION"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='THE_NAMESPACE_OF_YOUR_SCHEMA']">
<jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:javaType name="java.util.Date" xmlType="xs:date"
parseMethod="org.apache.cxf.tools.common.DataTypeAdapter.parseDate"
printMethod="org.apache.cxf.tools.common.DataTypeAdapter.printDate"/>
</jxb:globalBindings>
</jaxws:bindings>
</jaxws:bindings>
您可以参考http://cxf.apache.org/docs/wsdl-to-java.html部分“如何将xsd:dateTime映射到java.util.Date?”了解更多详情。
答案 3 :(得分:0)
我从2012年开始发表我的评论,现在我觉得有必要添加一个答案。我对Web服务进行了一些更改,遗憾的是,必须继续在Java 6上运行。我被要求禁止XML响应中所有日期和时间字段的时区/偏移部分。我使用JAXB绑定文件和3对适用于日期,时间和日期时间XML类型的适配器方法。请注意,我的解决方案使用JodaTime库。
这是我的JAXB 2.1绑定文件:
<?xml version="1.0" encoding="UTF-8"?>
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
version="2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<globalBindings>
<javaType
name="org.joda.time.DateTime"
xmlType="xs:dateTime"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseDateTime"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printDateTime" />
<javaType
name="org.joda.time.DateTime"
xmlType="xs:date"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseDate"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printDate" />
<javaType
name="org.joda.time.LocalTime"
xmlType="xs:time"
parseMethod="com.jimtough.jaxb.DataTypeCondapter.parseTime"
printMethod="com.jimtough.jaxb.DataTypeCondapter.printTime" />
</globalBindings>
</bindings>
这是我的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);
}
}