如何告诉 Apache CXF 在 Spring 配置中使用 java.util.Date
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3901415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell Apache CXF to use java.util.Date in Spring Configuration
提问by Iravanchi
I am Using CXF to host web services in a Spring context, which makes JAX-WS the default binding. And I'm using Java-First, which means annotated endpoint interfaces and classes.
我正在使用 CXF在 Spring 上下文中托管 Web 服务,这使 JAX-WS 成为默认绑定。我使用的是 Java-First,这意味着带注释的端点接口和类。
Since default binding for JAX-WS uses XMLGregorianCalendar
class for dates, when I call my web service passing a java.util.Date
it is converted to XMLGregorianCalendar
on the server.
由于 JAX-WS 的默认绑定使用XMLGregorianCalendar
日期类,当我调用我的 web 服务时,java.util.Date
它会XMLGregorianCalendar
在服务器上转换为。
There are many posts and documentation on how to change this to bind date values to java.util.Date
, but all are related to wsdl2java tool, such as:
有很多关于如何更改它以将日期值绑定到 的帖子和文档java.util.Date
,但都与 wsdl2java 工具有关,例如:
<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:dateTime"
parseMethod="org.apache.cxf.tools.common.DataTypeAdapter.parseDateTime"
printMethod="org.apache.cxf.tools.common.DataTypeAdapter.printDateTime"/>
</jxb:globalBindings>
</jaxws:bindings>
</jaxws:bindings>
Since I'm using Spring, I'm looking for a way to do this in Spring context configuration files (or CXF configuration files). A snippet of my file:
由于我使用的是 Spring,我正在寻找一种在 Spring 上下文配置文件(或 CXF 配置文件)中执行此操作的方法。我的文件的一个片段:
<jaxws:endpoint id="jaxwsDocumentGroupWsEndpoint" implementor="#documentGroupWsEndpoint" address="/documentGroup">
<!-- SOMETHING TO WRITE HERE TO TELL CXF TO USE java.util.Date ??? -->
</jaxws:endpoint>
回答by AValchev
The solution for your problem is hidden into class generation.
您的问题的解决方案隐藏在类生成中。
You should generate your classes with the following binding:
您应该使用以下绑定生成您的类:
<jaxb:globalBindings generateMixedExtensions="true">
<jaxb:javaType
name="java.util.Calendar"
xmlType="xs:dateTime"
parseMethod="com.test.DataTypeBinder.unmarshalDateTime"
printMethod="com.test.DataTypeBinder.marshalDateTime" />
</jaxb:globalBindings>
And include in your class path the following class:
并在您的类路径中包含以下类:
public class DataTypeBinder {
private static DateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
private static DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
public static Calendar unmarshalDate(String value) {
if (value == null || value.length() == 0) {
return null;
}
Date d = null;
try {
d = date.parse(value);
} catch (Exception e) {
e.printStackTrace();
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
public static String marshalDate(Calendar value) {
if (value == null) {
return null;
}
return date.format(value.getTime());
}
public static Calendar unmarshalDateTime(String value) {
if (value == null || value.length() == 0) {
return null;
}
Date d = null;
try {
d = dateTime.parse(value);
} catch (Exception e) {
e.printStackTrace();
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
}
Then JAXB is going to include in your generated classes the following type declaration:
然后 JAXB 将在您生成的类中包含以下类型声明:
@XmlElement(type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected Calendar transactionDate;