10

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));
Tristan
  • 8,733
  • 7
  • 48
  • 96

3 Answers3

17

Even better, since LocalDate is immutable, there's no harm in using the same object in the source and target beans. You can configure your MapperFactory as follows:

mapperFactory.getConverterFactory().registerConverter(new PassThroughConverter(LocalDate.class));
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
  • The source and the target are not beans, and I don't see how an immutable object could be reused with a new value since it's "immutable" (Immutable objects are simply objects whose state (the object's data) cannot change after construction). – Tristan Sep 01 '15 at 13:30
  • Have you tried running it with the PassThroughConverter? What I understand, you don't want to have have a LocalDate with a new value - you want the target LocalDate to have the same value as the source LocalDate. Since LocalDate is immutable, you don't need to create a new LocalDate object to achieve that - you can use the same object for target as for source. – Adam Michalik Sep 01 '15 at 13:39
  • mmm ok, you're right, I forgot about my use case. If it works, your solution is cleaner. I don't know why I wrote "return (LocalDate.from(source))" instead of "return source" – Tristan Sep 01 '15 at 21:39
9

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());
Tristan
  • 8,733
  • 7
  • 48
  • 96
1

OrikaMapper has fixed this in the 1.5.1 release. You can upgrade your dependency to 1.5.1 and it should be added there automatically. No need of adding a converter. Here are the release notes for 1.5.1: https://github.com/orika-mapper/orika/issues/179

PR for the fix: https://github.com/orika-mapper/orika/pull/178

        <dependency>
            <groupId>ma.glasnost.orika</groupId>
            <artifactId>orika-core</artifactId>
            <version>1.5.1</version>
        </dependency>
Karan Mehta
  • 53
  • 1
  • 15