How can you specify a defaultValue when mapping a DTO using MapStruct? The following constructs did not work
@Mapping(target = "used", defaultValue = "0")
MyDTO toDto(MyEntity entity);
@Mapping(target = "used", defaultValue = "false")
MyDTO toDto(MyEntity entity);
The documentation shows examples for all types except booleans
To be able to use defaultValue, you must specify a source. If the source is null then it will use the defaultValue defined.
To always assign a specific value to the target, in your case zero or false, you should then use constant instead.
More info on Default values and constants
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With