Skip to main content

MapStruct

MapStruct with Spring Boot 4 and Java Records: DTO Mapping Done Right

A beginner-first walk through MapStruct on Spring Boot 4.1.2 with Java records as DTOs: how the generated mapper works, nested and collection mapping, null-value strategies, updating existing objects, and what @AfterMapping, @BeforeMapping and @ObjectFactory really do when the target is an immutable record. Every figure comes out of a companion project whose 19 tests write the transcripts, plus the compiler's own error messages and an honestly labelled, indicative comparison with hand-written code and Dozer. Includes surprises: @MappingTarget on a record compiles to an empty method, and JDK 23+ no longer runs a processor that is only on the classpath.