将wsdl生成的XmlGregorianCalendar类型的日期替换为java.util.Date? [英] Replacing date of type XmlGregorianCalendar generated by wsdl to java.util.Date?

查看:508
本文介绍了将wsdl生成的XmlGregorianCalendar类型的日期替换为java.util.Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我们使用Spring webservices使用wsdl生成的文件调用第三方SAP系统。

I am working on an application in which we give call to Third Party SAP system using files generated through wsdl using Spring webservices.

使用wsdl生成的文件之一通过ws import具有类型为XMLGregorianCalendar的Date属性,作为响应,我们获得相应字段的空值。

One of the file generated using wsdl through ws import have Date attribute of type "XMLGregorianCalendar" and in response we are getting null value for corresponding field .

我想将日期从XmlGregorianCalendar转换为java.util。日期。

I want to convert date from XmlGregorianCalendar to java.util.Date.

已提及:如何更换XmlGregorianCalendar按日期?
但无法通过wsdl提供适当的xjb绑定。

Have referred : how replace XmlGregorianCalendar by Date? but not able to provide appropriate xjb bindings through wsdl.

如果有人可以建议转换由wsdl生成的日期,这将是非常有帮助.....
在此先感谢!
Shuchi

If anyone can suggest the conversion of Dates generated by wsdl ,it would be of great help..... Thanks In Advance ! Shuchi

推荐答案

WSDL与xjb无关。 xjb用于作为-b参数传递的xjc编译器。
即。

WSDL has no deal with xjb. xjb is for xjc compiler passed as -b parameter. i.e.

xjc -b <file>

文档:自定义JAXB绑定

如果使用Maven插件生成您的JAXB Java类,其中任何一个都具有绑定配置,即

if you use Maven plugins to generate your JAXB Java classes any of them have Binding configuration i.e.

<groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <configuration>
                    <defaultOptions>
                        <bindingFiles>
                            <bindingFile>${project.interfaces.basedir}Configuration/Bindings/common-binding.xjb</bindingFile>
                        </bindingFiles>

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/XMLSchema</schemaDirectory>
                <bindingDirectory>${basedir}/src/main/resources/Bindings</bindingDirectory>
            </configuration>

依旧......

xjb非常简单:

<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings>
    <jaxb:serializable uid="1" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:dateTime"
        parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
        printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:date"
        parseMethod="javax.xml.bind.DatatypeConverter.parseDate" printMethod="javax.xml.bind.DatatypeConverter.printDate" />
    <jaxb:javaType name="java.util.Calendar" xmlType="xsd:time"
        parseMethod="javax.xml.bind.DatatypeConverter.parseTime" printMethod="javax.xml.bind.DatatypeConverter.printTime" />            
</jaxb:globalBindings>

你可以看到它定义从xsd:dateTime,xsd:date和xsd:time types转换为java.util.Calendar。

as you can see it defines conversions from xsd:dateTime, xsd:date and xsd:time types to java.util.Calendar.

我不建议使用java.util.Date。日期处理存在许多问题(特别是对于不同的时区)。
最好使用java.util.Calendar。日历更容易处理,JDK中有默认的转换器实现:

I do not recommend to use java.util.Date. There are many troubles with Date handling (especially with different timeZones). It is better to use java.util.Calendar. Calendar is much easier to handle and default converter implementation is there in JDK:

javax.xml.bind.DatatypeConverter

但是,如果你仍然想使用java.Util.Date,你需要拥有自己的两个静态方法的小转换器解析和打印,然后在xjb中设置它。 ie

But, if you still want to use java.Util.Date you need to have your own small converter with two static methods "parse" and "print" and then set it in xjb. i.e.

public class MyDateConverter {
    public static java.util.Date parse(String xmlDateTime) {
        return javax.xml.bind.DatatypeConverter.parseDateTime(xmlDateTime).getTime();
    }

    public static String print(Date javaDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(javaDate.getTime());
        return javax.xml.bind.DatatypeConverter.printDateTime(calendar);
    }
}

您在xjb中的转换将如下所示:

your conversions in xjb will look like:

<jaxb:javaType name="java.util.Date" xmlType="xsd:dateTime"
    parseMethod="MyDatatypeConverter.parse"
    printMethod="MyDatatypeConverter.print" />

这篇关于将wsdl生成的XmlGregorianCalendar类型的日期替换为java.util.Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆