Java / MapStruct Java Interview questions part 2
What is the purpose of the @Mapping annotation?
@Mapping configures how a single target property is populated when the default name-matching rule does not apply. It is placed on the mapping method, not on the fields themselves.
@Mapping(source = "fullName", target = "name") @Mapping(target = "id", ignore = true) PersonDto toDto(Person person);
Common attributes include source/target for renaming, ignore to skip a property, expression for a Java snippet, constant for a fixed value, and dateFormat for formatting date/time fields during conversion. You can stack several @Mapping annotations on the same method, one per property that needs special handling, while every other property still maps automatically by name.
More Related questions...