MapStruct编译报错:源参数不存在「Id」「remark」属性问题排查及@Data注解使用疑问
Let's break down your problems and fix them step by step:
1. Fixing the MapStruct Compilation Error
The error message says MapStruct can't find properties named Id and remark in your source parameters — here's why, plus actionable fixes:
a. Fix Naming Convention Mismatch
Your entity/DTO fields use Id (uppercase first letter), but MapStruct resolves properties based on JavaBean getter/setter rules. For a field named Id, Lombok generates a getter getId() — MapStruct maps this to the property name id (lowercase first letter), not Id.
Quick fix: Update your @Mapping annotations to use lowercase id instead of uppercase Id. For better long-term consistency, rename your fields to follow standard Java naming (e.g., id instead of Id) to avoid similar issues down the line.
b. Enforce Correct Annotation Processor Order
MapStruct needs Lombok-generated getters/setters to map fields properly, so you must ensure Lombok runs before MapStruct during compilation. Add this configuration to your pom.xml to lock in the order:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source><!-- Match your JDK version, e.g., 11 --></source> <target><!-- Match your JDK version --></target> <annotationProcessorPaths> <!-- Run Lombok first --> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version><!-- Your Lombok version, e.g., 1.18.30 --></version> </path> <!-- Then run MapStruct --> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version><!-- Your MapStruct version, e.g., 1.5.5.Final --></version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
c. Simplify Your Mapper (Optional)
Once you fix the naming, your source and target fields will match exactly — you can remove the redundant @Mapping annotations entirely, since MapStruct auto-matches fields with identical names:
@Mapper(imports = { Instant.class, DateTimeFormatter.class }) public interface CommentMapper { CommentMapper INSTANCE = Mappers.getMapper(CommentMapper.class); Comment getComment(CommentDto commentDto); // Renamed parameter for clarity CommentDto getCommentDto(Comment comment); }
d. Remove Unnecessary @Configuration
Your CommentMapper interface doesn't need the @Configuration annotation — that's for Spring beans, and MapStruct uses its own instance via Mappers.getMapper(). Removing it will avoid confusion.
2. Is Using Only @Data Enough for Getters & Setters?
Absolutely! The @Data annotation is a Lombok composite annotation that includes all the functionality you need, plus extra utility methods:
@Getter(generates getters for all fields)@Setter(generates setters for all non-final fields)@ToString(generates a human-readable toString method)@EqualsAndHashCode(generates equals/hashCode methods for object comparison)@RequiredArgsConstructor(generates a constructor for final/@NonNull fields)
If you only need getters and setters, @Data is more than sufficient — it's the most concise option for most projects. A couple of quick notes:
- For JPA entities like your
PostandCommentclasses,@Datais safe, but you may want to tweak@EqualsAndHashCodeto exclude lazy-loaded associations (e.g.,@EqualsAndHashCode(exclude = "comments")onPost) to avoid circular reference issues. - If you don't want the extra utility methods, you can use
@Getter + @Setterinstead, but@Datais the standard choice for most developers.
内容的提问来源于stack exchange,提问作者Oxana Grey

