Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Apache CXF to use java.util.Date in Spring Configuration

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.

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.

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:

<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:

<jaxws:endpoint id="jaxwsDocumentGroupWsEndpoint" implementor="#documentGroupWsEndpoint" address="/documentGroup">
<!-- SOMETHING TO WRITE HERE TO TELL CXF TO USE java.util.Date ??? -->
</jaxws:endpoint>
like image 523
Iravanchi Avatar asked Oct 10 '10 17:10

Iravanchi


1 Answers

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:

@XmlElement(type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlSchemaType(name = "dateTime")
protected Calendar transactionDate;
like image 119
Valchev Avatar answered Oct 12 '22 22:10

Valchev