WSDL自定义:XMLGregorianCalender到java.util.Date [英] WSDL customization: XMLGregorianCalender to java.util.Date

查看:121
本文介绍了WSDL自定义:XMLGregorianCalender到java.util.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个几百个wsdl文件。每当我为它们创建客户端jaxb客户端类时,Jaxb会自动将所有日期/时间字段映射到XMLGregorianCalender。经过大量的谷歌搜索,我发现提供单独的绑定文件是唯一的解决方案。

I have several wsdl files almost hundreds. Whenever I create client jaxb client classes for them the Jaxb automatically maps all the date/time fields to XMLGregorianCalender. After a lot of googling, I found out providing a separate binding file is the only solution.

我不想提供wsdl位置,因为我已经这样做了很多,否则我必须为每个wsdl创建一个单独的绑定文件。

I don't want to provide the wsdl location, since I've so many, as otherwise I'd have to create a separate binding files for each wsdl.

下面是我使用的绑定文件。

Below is the binding file I used.

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" >
    <globalBindings>
        <javaType name="java.util.Date" xmlType="xsd:dateTime"  />
    </globalBindings>
</bindings>

它创建了带有Date类型的jaxb类,但它还自动创建了一个名为Adapter1.java的适配器放入我不想要的。我有自己的包结构,不能偏离它。

It created the jaxb classes with Date types but it also created an adapter called Adapter1.java automatically which was put in which I don't want. I've my own package structure and can't deviate from it.


org.w3._2001.xmlschema

org.w3._2001.xmlschema

并且此适配器将日期从String转换为java.util.Date,我的应用程序失败,因为转换器应该从XMLGregorianCalender转换为java.util.Date

and this adapter converts the date from String to java.util.Date and my application fails as the converter should convert from XMLGregorianCalender to java.util.Date

所以,我自己写了一个适配器

So, I wrote an Adapter myself

import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import java.util.Calendar;
import javax.xml.bind.annotation.adapters.XmlAdapter;



public class DateAdapter extends XmlAdapter<XMLGregorianCalendar, Date> {

    @Override
    public XMLGregorianCalendar marshal(Date date) throws Exception {
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        gregorianCalendar.setTime(date);
        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        return xmlGregorianCalendar;
    }

    @Override
    public Date unmarshal(XMLGregorianCalendar xmlGregorianCalendar) throws Exception {
        return xmlGregorianCalendar.toGregorianCalendar().getTime();
    }

}

并更改了我的自定义文件:

and changed my customization file like this:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" >
    <globalBindings>
        <javaType name="java.util.Date" xmlType="xsd:dateTime"
        parseMethod="DateAdapter.marshal"
            printMethod="DateAdapter.unmarshal" />
    </globalBindings>
</bindings>

然后我运行了wsimport工具,但它失败了。

Then I ran the wsimport tool and it failed.

C:\Users\stuart\Desktop\code>wsimport -s src -d gen -b cust.txt http://localhost:8080/webservice-jaxws/DummyService?wsdl


parsing WSDL...


generating code...


compiling code...

C:\Users\stuart\Desktop\code\src\org\w3\_2001\xmlschema\Adapter1.java:13: cannot find symbol
symbol  : variable DateAdapter
location: class org.w3._2001.xmlschema.Adapter1
        return (DateAdapter.marshal(value));
                ^
C:\Users\stuart\Desktop\code\src\org\w3\_2001\xmlschema\Adapter1.java:17: cannot find symbol
symbol  : variable DateAdapter
location: class org.w3._2001.xmlschema.Adapter1
        return (DateAdapter.unmarshal(value));
                ^
2 errors
compilation failed, errors should have been reported

我将自定义设置保存在custsi.txt中,如wsimport命令中所示,我的DateAdapter类源文件也在同一目录中。班级没有包裹。以下是我的目录结构。

And I kept my customization settings in cust.txt as given in wsimport command, and my DateAdapter class source file was also in the same directory. The class was without package. The following is my directory structure.

³   cust.txt
³   DateAdapter.java
³   
ÃÄÄÄgen
³   ÃÄÄÄorg
³   ³   ÀÄÄÄw3
³   ³       ÀÄÄÄ_2001
³   ³           ÀÄÄÄxmlschema
³   ³                   Adapter1.class
³   ³                   
³   ÀÄÄÄwebservice
³       ÀÄÄÄjaxws
³           ÀÄÄÄgenerated
³                   GetBook.class
³                   GetBookResponse.class
³                   ObjectFactory.class
³                   package-info.class
³                   Book.class
³                   BookService.class
³                   BookServiceImpl.class
³                   ReturnBook.class
³                   ReturnBookResponse.class
³                   
ÀÄÄÄsrc
    ÃÄÄÄorg
    ³   ÀÄÄÄw3
    ³       ÀÄÄÄ_2001
    ³           ÀÄÄÄxmlschema
    ³                   Adapter1.java
    ³                   
    ÀÄÄÄwebservice
        ÀÄÄÄjaxws
            ÀÄÄÄgenerated
                    GetBook.java
                    GetBookResponse.java
                    ObjectFactory.java
                    package-info.java
                    Book.java
                    BookService.java
                    BookServiceImpl.java
                    ReturnBook.java
                    ReturnBookResponse.java


推荐答案

使用您所做的相同绑定声明解决了这个问题,但我的DateAdapter实际上看起来像这样:

Solved this by using the same bindings declaration you did except my DateAdapter actually looked like this :

public class DateAdapter {

    private DateAdapter() {}

    public static String marshal(Date date) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        return DatatypeConverter.printDateTime(cal);
    }

    public static Date unmarshal(String xmlDate) {
        return DatatypeConverter.parseDate(xmlDate).getTime();
    }
}

它就像一个魅力。 XML方面我有dateTime和java方面我有java.util.Date。我实际上做了另一个使用java.time.Instant而不是Date,我发现它更容易使用。

And it works like a charm. XML side I have dateTime and java side I have java.util.Date. I actually made another that uses java.time.Instant instead of Date, which I find easier to work with.

这篇关于WSDL自定义:XMLGregorianCalender到java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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