Java / MapStruct Java Interview questions part 2
Why should you use MapStruct instead of hand-written DTO mapping code?
Hand-written mapping code is straightforward at first, but it scales poorly: every new field added to an entity or DTO has to be remembered and updated in every mapping method by hand, and a forgotten field fails silently since plain Java won't warn you about a property nobody copied.
MapStruct keeps essentially the same runtime characteristics as hand-written code - no reflection, direct getter/setter calls - while adding compile-time visibility into exactly which properties were and weren't mapped. Adding a field to a DTO and forgetting to wire it up now produces a build-time warning (or error) pointing at the exact method and property, instead of a silent bug discovered later in production.
It also removes a large amount of repetitive boilerplate, since simple one-to-one property copies require no code at all, only a method signature.
More Related questions...