Skip to content
Advertisement

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));

Advertisement

Answer

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());
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement