Skip to content
Advertisement

Mapstruct how to initialize fields

I have a DTO like this

JavaScript

and an entity

JavaScript

I want to create a mapper to map DTO to entity. How can I make the field private Instant timestamp; has value like Instant.now()?

My mapper so far

JavaScript

It got compile error

JavaScript

Advertisement

Answer

The issue is due to the source being empty. Remove the source and use expression instead of defaultExpression.

JavaScript

When there is a source but the source is null upon resolving, then defaultExpression comes handy to assign a value to the target.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement