Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a default value for a boolean field using MapStruct

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

like image 425
Omtara Avatar asked Mar 05 '26 17:03

Omtara


1 Answers

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

like image 85
atish.s Avatar answered Mar 07 '26 06:03

atish.s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!