Search code examples
javadateorikaxmlgregoriancalendar

Is it possible to override Orika's built in converter - XMLGregorianCalendar to java.util.Date?


Orika has a set of "BuiltinConverters"

https://github.com/orika-mapper/orika/blob/master/core/src/main/java/ma/glasnost/orika/converter/builtin/BuiltinConverters.java

But the one that maps XMLGregorianCalendar to java.util.Date does not work properly for me. I discovered the way to map it with a satisfying result, but I cannot find a way to make Orika use it instead of its own built in converter. I tried registering the converter in MapperDefinition:

public class MapperDefinition {

   private final MapperFactory factory;
   private final ConverterFactory converterFactory;

   public MapperDefinition() {

     factory = new DefaultMapperFactory.Builder().build();
     converterFactory = factory.getConverterFactory();

     converterFactory.registerConverter(new CustomConverter<XMLGregorianCalendar, Date>() {
       @Override
       public Date convert(XMLGregorianCalendar source, Type<? extends Date> destinationType) {
         LocalDateTime ldt = LocalDateTime.of(source.getYear(), source.getMonth(), 
             source.getDay(), source.getHour(), source.getMinute(), source.getSecond());
         return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
       }
     }
   }

}

But debugging shows that Orika seems to ignore my custom converter and still uses its own built-in converter. I also cannot find any information about overriding it on Orika User Guide:

https://orika-mapper.github.io/orika-docs/converters.html

Does anyone know if it's possible to make Orika use my way of mapping those two types?


Solution

  • I found the answer myself.

    The thing is, the built-in converter is a "BidirectionalConverter" and I used "CustomConverter", because I was only interested in changing mapping in one way: XML --> Date. Changing it to bidirectional solved the problem.