Search code examples
javaorika

How to map java.time.LocalDate field with Orika?


This occurs because LocalDate is not a JavaBean (it has no zero-arg constructor)

To fix this, you need to create a LocalDateConverter :

public class LocalDateConverter extends BidirectionalConverter<LocalDate, LocalDate> {

  @Override
  public LocalDate convertTo(LocalDate source, Type<LocalDate> destinationType) {
    return (source);
  }

  @Override
  public LocalDate convertFrom(LocalDate source, Type<LocalDate> destinationType) {
    return (source);
  }

}

and then register it adding this line :

mapperFactory.getConverterFactory().registerConverter(new LocalDateConverter());

As a shorcut, you can instead register the provided "PassThroughConverter" as suggested by Adam Michalik so Orika doesn't try to instanciate a new "LocalDate" :

mapperFactory.getConverterFactory().registerConverter(new PassThroughConverter(LocalDate.class));

Solution

  • This occurs because LocalDate is not a JavaBean (it has no zero-arg constructor)

    To fix this, you need to create a LocalDateConverter :

    public class LocalDateConverter extends BidirectionalConverter<LocalDate, LocalDate> {
    
      @Override
      public LocalDate convertTo(LocalDate source, Type<LocalDate> destinationType) {
        return (LocalDate.from(source));
      }
    
      @Override
      public LocalDate convertFrom(LocalDate source, Type<LocalDate> destinationType) {
        return (LocalDate.from(source));
      }
    
    }
    

    and then register it adding this line :

    mapperFactory.getConverterFactory().registerConverter(new LocalDateConverter());