Java / MapStruct Java Interview questions part 2
What is the difference between qualifiedByName and qualifiedBy?
Both attributes disambiguate which custom method MapStruct should call when more than one candidate method matches the same source/target type pair - they differ in how the target method is identified.
| qualifiedByName | qualifiedBy |
References a method by the string given in its @Named("...") annotation. | References a method by a custom annotation type used as a marker. |
| Simple, one string per method - easy for small numbers of overloads. | Type-safe (refactor-friendly), scales better with many qualifiers. |
@Mapping(qualifiedByName = "trim") | @Mapping(qualifiedBy = Trim.class) |
Both approaches solve the exact same ambiguity problem; teams generally pick qualifiedByName for a handful of helper methods and move to custom qualifiedBy annotations once a mapper accumulates many overlapping conversion methods. A custom annotation also documents intent directly in the method signature, which can make a large mapper easier to navigate than a list of string labels scattered across @Named declarations.
More Related questions...