Question

I’m trying to register a custom converter for XMLGregorianCalendars to do the conversión from POJO received by WS (with XMLGregorianCalendar fields) to my model (Date objects).

I tried to register to process properties with XMLGregorianCalendar.class, but the it seems to be an anonymous class and in executing time is detected as XMLGregorianCalendarImpl.class, which doesn’t cause the triggering of my custom converter.

How can I do this conversion? I can register to XMLGregorianCalendarImpl.class because it is not a “visible” class…

Here it's my code:

[…]
ConvertUtils.register(new XMLGregorianCalendarConverter(), XMLGregorianCalendar.class);
BeanUtils.copyProperties(result, original);
[…] //”result” is a object from my model with Date properties and “original” a pojo with properties with the same name but with XMLGregorianCalendar.

XMLGregorianCalendarConverter.class:

import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.commons.beanutils.converters.DateConverter;

public class XMLGregorianCalendarConverter implements org.apache.commons.beanutils.Converter{

       private DateConverter dateConverter = new org.apache.commons.beanutils.converters.DateConverter();

       @SuppressWarnings("unchecked")
       @Override
       public <T> T convert(Class<T> type, Object value) {
             if(value == null || value.toString().length() < 1)
                    return null;

             if(type.toString().contains("XMLGregorianCalendar")){
                    XMLGregorianCalendar xmldate = (XMLGregorianCalendar) value;
                    return (T) xmldate.toGregorianCalendar().getTime();
             }else{
                    return dateConverter.convert(type, value);
             }
       }

}

Thanks!

Was it helpful?

Solution

Instead of working with XMLGregorianCalendar you could use a customization file:

https://jaxb.java.net/guide/Using_different_datatypes.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top