Skip to content
Advertisement

MapStruct – Can’t map property Optional to LocalDate

NOTE: This is unlike other questions on StackOverflow because they resolve this issue by mapping the two classes manually. Since ScheduleSource and ScheduleTarget are exactly the same classes, I want them to be mapped automatically.

Hi,

I have 2 classes ScheduleSource and ScheduleTarget. They have exactly the same properties.

When I try to use MapStruct to map from ScheduleSource to ScheduleTarget, I get the error:

JavaScript

I have attached the two files. Can you please help?

Files are:

  • ScheduleSource, ScheduleTarget – the two Java Beans
  • ScheduleMapper – the mapping class.

  • ScheduleMapper.java

JavaScript
  • ScheduleSource.java, ScheduleTarget.java – same structure
JavaScript

Advertisement

Answer

I’m not familiar with mapstruct, but I can guess it maps different objects 🙂

If your source and target classes have the same structure then the problem is

JavaScript

So it gets the Optional object and tries to pass it to a method accepting a LocalDate.

So your possible ways of action are

  1. change getter to return a simple object
  2. change setter to accept an optional (which is fine I guess, but seems a bit off)
  3. declare a mapper method
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement