首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Apache CXF在没有时区的情况下表示日期?

如何使用Apache CXF在没有时区的情况下表示日期?
EN

Stack Overflow用户
提问于 2011-05-27 02:18:07
回答 3查看 24.7K关注 0票数 17

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

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

当CXF呈现包含它的XML文档时,它会以这种形式呈现它(其中-06:00表示时区):

2000-01-18-06:00

如何将CXF配置为不呈现时区?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-27 03:01:48

缺省情况下,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?”了解更多详细信息。

票数 4
EN

Stack Overflow用户

发布于 2011-08-18 23:27:46

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

不要问我为什么在每个合理的逻辑中-当将日期编组到xs: XMLgregorianCalendar时,它会保留时区。

我一直在想-时区可以更适用于xs:dateTime,但我所知道的是...关于类型。

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

票数 64
EN

Stack Overflow用户

发布于 2017-02-23 06:23:22

我在上面找到了2012年的评论,现在我觉得有必要补充一个答案。我正在对一个web服务进行一些更改,不幸的是,它必须继续运行在Java 6上。我被要求在XML响应中取消所有日期和时间字段的时区/偏移部分。我用一个JAXB绑定文件和3对用于date、time和datetime 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);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6143224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档