Axis2 JAXWS service xs:date XMLGregorianCalendar

I'm using axis2-1.6.2 with JAX-WS RI 2.2.3.

I'm using WSDL to generate skeleton,stub and model classes. Following is the code snipset of WSDL.

<xs:complexType name="dailyBooking">
<xs:sequence>
<xs:element minOccurs="0" name="day" qualified="true" type="xs:date" />
<xs:element minOccurs="0" name="noOfBookings" type="xs:int" />
</xs:sequence>
</xs:complexType>

It generates following class

public class DailyBooking {

    protected Integer noOfBookings;
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar day;

I would like to generate java.util.Date instead of XMLGregorianCalendar. How it possible.

Upvotes: 1

Views: 776

Answers (1)

kolossus
kolossus

Reputation: 20691

The quick and dirty way to achieve this is to specify an annotation in your xsd that will instruct JAXB what types to convert and how to convert them. Try this:

  1. Add the JAXB namespace to the xsd file (or the wsdl itself section if your type definitions are embedded in the wsdl)

    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    jaxb:version="2.0"
    
  2. Add the following annotation element either as a child of the <schema/> element or in the xsd file

       <xs:annotation>
          <xs:appinfo>
              <jaxb:globalBindings mapSimpleTypeDef="false" choiceContentProperty="true">
                 <jaxb:javaType name="java.util.Date" xmlType="xs:date"
                       parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
                       printMethod="javax.xml.bind.DatatypeConverter.printDate"/>
                  </jaxb:globalBindings>
          </xs:appinfo>
       </xs:annotation>
    

A few notes:

  1. The javaType element specifies the java datatype you wish to convert to and

  2. xmlType specifies it's corresponding occurrence in your schema definition.

  3. Using globalBindings means that all occurrences of that xml type will be subject to the customization you've specified

Upvotes: 1

Related Questions