1

I have to use some SOAP web services API that I have no control over it. I am using Apache Axis2 to generate java classes (from WSDL) and Axis2 use'java.util.Calendar' for date time data type.

The problem here is API doesn't recognize the date time with TimeZone 2016-12-31T12:00:00.000+06:30 when Axis2 parse it to String and send request. That API only knows that format 2016-12-31T12:00:00. I have tried calendar.clear(Calendar.ZONE_OFFSET); but it still render TimeZone in XML request. I tested with XMLGregorianCalendar instead of Calendar and it worked but I needed to change Calendar data type to XMLGregorianCalendar in generated classes and those changes might be gone if there would be a newer WSDL version and we generate new classes again.

Any idea? Thanks in advance.

Ye Kyaw Kyaw Htoo
  • 702
  • 2
  • 9
  • 22

2 Answers2

2

I found the answer from this but not from the first answer. I solved the problem (as MichaƂ Niklas's answer) by creating own CustomConverterUtils by extendingorg.apache.axis2.databinding.utils.ConverterUtil and remove appendTimeZone() method call in convertToString(Calendar value). I also had to change appendTime(Calendar value, StringBuffer dateString) method. My CustomConverterUtils is as follow:

public class CustomeConverterUtils extends ConverterUtil {

     public static String convertToString(Calendar value) {
                if (value.get(Calendar.ZONE_OFFSET) == -1){
                    value.setTimeZone(TimeZone.getDefault());
                }
                StringBuffer dateString = new StringBuffer(28);
                appendDate(dateString, value);
                dateString.append("T");
                //adding hours
                appendTime(value, dateString);

                return dateString.toString();
        }

        public static void appendTime(Calendar value, StringBuffer dateString) {
            if (value.get(Calendar.HOUR_OF_DAY) < 10) {
                dateString.append("0");
            }
            dateString.append(value.get(Calendar.HOUR_OF_DAY)).append(":");
            if (value.get(Calendar.MINUTE) < 10) {
                dateString.append("0");
            }
            dateString.append(value.get(Calendar.MINUTE)).append(":");
            if (value.get(Calendar.SECOND) < 10) {
                dateString.append("0");
            }
           dateString.append(value.get(Calendar.SECOND));
        }
}

And you also need to put those codes too. I needed to put in my Application class as I am now using Spring Boot.

public static void main(String[] args) throws Exception {
    String convert_class = "com.ykkh.test.CustomeConverterUtils";
    System.setProperty(ConverterUtil.SYSTEM_PROPERTY_ADB_CONVERTERUTIL, convert_class);
    SpringApplication.run(Application.class, args);
    }
Community
  • 1
  • 1
Ye Kyaw Kyaw Htoo
  • 702
  • 2
  • 9
  • 22
1

A small suggestion would be to get the name from the Class rather than hardcoding it as a string. This will help us to get a compilation error, in case we change the name or package for the CustomConverterUtil class.

            // axis2 changes for customising the converters
        String axis2_conversion_class = CustomConverterUtils.class.getName();
        System.setProperty(ConverterUtil.SYSTEM_PROPERTY_ADB_CONVERTERUTIL, axis2_conversion_class);

In case you are upgrading from Axis1 to Axis2, use below code for conversion:

public class CustomConverterUtils extends org.apache.axis2.databinding.utils.ConverterUtil {

private static SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

static {
    zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
}

/**
 * taking logic from org.apache.axis.encoding.ser.CalendarSerializer
 * @param calendar
 * @return
 */
@NotNull
public static String convertToString(Calendar calendar) {
    Date date = calendar.getTime();
    synchronized(zulu) {
        return zulu.format(date);
    }
}

@NotNull
public static String convertToString(Date date) {
    synchronized(zulu) {
        return zulu.format(date);
    }
}

}

Mathew Stephen
  • 309
  • 2
  • 7