Java / MapStruct Java Interview questions part 2
How do you use @InheritConfiguration to reuse mapping rules?
@InheritConfiguration copies the @Mapping configuration from one method onto another method that maps the same (or compatible) source and target types, avoiding duplicated annotations across near-identical methods.
@Mapping(source = "empName", target = "name") EmployeeDto toDto(Employee employee); @InheritConfiguration EmployeeDto toDtoForReport(Employee employee);
This is commonly used when you need two methods with the same mapping rules but different names - for example, one plain conversion method and one used specifically as an @AfterMapping hook target - without maintaining two separate, identical sets of @Mapping annotations that could drift out of sync over time.
More Related questions...