Sunday, September 27, 2009

Handling date and dateTime with Axis2

With Axis2 1.5 java.util.Date is mapped to xs:date type and java.util.Calendar is mapped to xs:dateTime. For an example if we deployed a POJO service as follows

public class TestService {

public Date getDate() {
return new Date();
}

public Calendar getCalendar() {
return Calendar.getInstance();
}
}

This generates an WSDL with schema like this.

<xs:element name="getDateResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getCalendarResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
When invoking the service we get the following response xmls

<ns:getDateResponse xmlns:ns="http://service.lockhead.test"><ns:return>2009-09-28</ns:return></ns:getDateResponse>
<ns:getCalendarResponse xmlns:ns="http://service.lockhead.test"><ns:return>2009-09-28T10:55:01.770+05:30</ns:return></ns:getCalendarResponse>

3 comments:

Oleg Zenzin said...

Hi Amila

thanks for this post!
Have you tried java.util.Date in the request? Like for instance getWeekDay(Date) for instance. This is where I have NumberFormatException during de-serialization on the server side because 19-chars length string is expected...

Anonymous said...

Thanks too.
Save me after hours of debugging

sundar1237 said...

You can use this method to convert your Calendar to String and easily set in the POJO class.

org.apache.axis2.databinding.utils.ConverterUtil.convertToString(Calendar.getInstance())