【问题标题】:Apache CXF how to handle date conversion xsd:date to java.util.Date without timezoneApache CXF 如何在没有时区的情况下处理日期转换 xsd:date 到 java.util.Date
【发布时间】:2015-08-05 23:39:01
【问题描述】:

这是我对 CXF 的问题。我有一个用 CXF 编写的 SOAP1.2 服务。该服务并不复杂,它基本上放置了一个 XML int DB (Oracle 11.x)。 WSDL 中的所有日期都定义为 xsd:date。

我定义了以下 jaxb 绑定

    <jxb:javaType name="java.util.Date" xmlType="xsd:date"
        parseMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDate"
        printMethod="org.apache.cxf.xjc.runtime.DataTypeAdapter.printDate" />

利用这些的Adapter类如下

public class Adapter2
extends XmlAdapter<String, Date>
{


public Date unmarshal(String value) {
    return (org.apache.cxf.xjc.runtime.DataTypeAdapter.parseDate(value));
}

public String marshal(Date value) {
    return (org.apache.cxf.xjc.runtime.DataTypeAdapter.printDate(value));
}
}

由于 cxf..runtime.DataTypeAdapter 总是产生 java.util.Date,所以总是添加一个时间,这反过来又给 Oracle 带来问题,因为 Oracle 用于 xml 验证的本机过程在遇到带时间的日期时会产生错误。 (更改数据库设置不是一种选择)。

可以使用哪些库/类来编组/解组 xsd:date 到没有时间的日期?还是我必须编写自己的类来扩展 XmlAdapter?

【问题讨论】:

    标签: java xsd jaxb cxf


    【解决方案1】:

    我想出了这个

    import java.util.Date;
    import java.text.SimpleDateFormat;
    
    import org.apache.cxf.xjc.runtime.DataTypeAdapter;
    
    public class DateTypeAdapterWrapper {
    
        private static final String DATE_FORMAT = "yyyy-MM-dd";
    
        public static Date parseDate(String value) {
            return (DataTypeAdapter.parseDate(value));
        }
    
        public static String printDate(Date value) {
            String tmp = DataTypeAdapter.printDate(value);
            if(tmp == null) {
                return null;
            }
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
            return dateFormat.format(value);
        }
    }
    

    毫无疑问,还有更好的方法,但它就是这样。

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 2020-06-02
      • 1970-01-01
      • 2020-08-19
      • 2018-10-04
      • 2019-09-28
      • 1970-01-01
      • 2020-04-09
      • 2022-11-12
      相关资源
      最近更新 更多