Mapstruct default value for LocalDate
Asked Answered
C

1

5

How to use defaultValue for LocalDate using Mapstruct? I was trying everything and nothing works. My failed attempts below:

@Mapping(target = "bornDate", defaultValue = "2019-10-11", dateFormat = "yyyy-MM-dd")
Person kidToPerson(Kid kid);

@Mapping(target = "bornDate", defaultExpression = "java(LocalDate.MIN)", dateFormat = "yyyy-MM-dd")
Person kidToPerson(Kid kid);

@Mapping(target = "bornDate", defaultExpression = "java(LocalDate.parse(2019-01-01))", dateFormat = "yyyy-MM-dd")
Person kidToPerson(Kid kid);

I just want to have some default value after mapping in my property LocalDate bornDate from Person.

Chunky answered 26/11, 2019 at 15:11 Comment(2)
could you post the source and target as well.Kailakaile
Why are those attempts failed? What kind of errors did you have?Dessertspoon
S
13

Try this

@Mapper(imports = {LocalDate.class})
public interface MyMapper {
    @Mapping(target = "bornDate", defaultExpression ="java(LocalDate.of(2019,12,31))")
    Person kidToPerson(Kid kid);
}
Sideburns answered 17/1, 2020 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.