Java / MapStruct Java Interview questions part 2
How do you perform custom type conversion in MapStruct?
For conversions MapStruct can't infer automatically, you provide the logic yourself as a default method (or a separate helper class referenced via uses), and MapStruct wires it in wherever the parameter/return types match a needed conversion.
@Mapper public interface InvoiceMapper { InvoiceDto toDto(Invoice invoice); default LocalDate toLocalDate(String raw) { return raw == null ? null : LocalDate.parse(raw, DateTimeFormatter.ISO_DATE); } }
When multiple candidate methods could apply to the same types, use @Named on the helper method and reference it from @Mapping(qualifiedByName = "...") to remove the ambiguity explicitly rather than relying on MapStruct's default resolution order.
More Related questions...