Hi Miguel,
Am 21.01.2014 17:07, schrieb Miguel Fernández Martínez:
> Hello,
>
>
>
> 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…
>
>
I had a look into the [beanutils] code, and indeed it does not seem to
be possible to register a converter for a base class and classes derived
from it. When looking up a converter for a given class always an exact
match is needed (actually it is a get operation on a hash map).
So there is no easy and official solution to this problem. You might
want to open an enhancement request in our bug tracking system [1], but
I cannot promise that this feature will be made available soon.
As a work-around: Would it be possible to do the registration with a
concrete XMLGregorianCalendar object at hand? If you had such an object,
you could do something like
ConvertUtils.register(new XMLGregorianCalendarConverter(),
myCalendarObj.getClass());
Oliver
>
> 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);
>
> }
>
> }
>
>
>
> }
>
>
>
>
>
> Thank you very much for your attention.
>
> Regards,
>
>
>
> Miguel
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]