源参数中不存在名为 "Id" 的 属性。您是说 "null" 吗?

No property named "Id" exists in source parameter(s). Did you mean "null"?

我有点陷入以下错误

Post.class

@Data
@Getter
@Setter
@Entity
@Table(name = "posts", uniqueConstraints = { @UniqueConstraint(columnNames = { "title" }) }

)

public class Post {

    @Id
    @Column(name = "Id", nullable = false)
    private Long Id;

    @Column(name = "title", nullable = false)
    private String title;

    @Column(name = "description", nullable = false)
    private String description;

    @Column(name = "content", nullable = false)
    private String content;

    @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Comment> comments = new HashSet<>();

Comment.class

    Data
    @Entity
    @Table(name = "postcomment")
    public class Comment {
    
        @Id
        @Column(name = "Id", nullable = false)
        private Long Id;
    
        @Column(name = "remark", nullable = false)
        private String remark;
    
        @ManyToOne(fetch = FetchType.LAZY, optional = false) // get only related comments to post
        @JoinColumn(name = "post_Id", nullable = false)
        @OnDelete(action = OnDeleteAction.CASCADE)
        private Post post;
    }

PostDto .class

@Data
public class PostDto {

    private Long Id;

    @NotEmpty
    @Size(min = 2, message = "post title should have atleast 2 characters")
    private String title;

    @NotEmpty
    @Size(min = 10, message = "post title should have atleast 2 characters")
    private String description;

    @NotEmpty
    @Size(min = 10, message = "post title should have atleast 2 characters")
    private String content;

CommentDto.class

@Data
public class CommentDto {

    private Long Id;
    private String remark;


}

CommentMapper.class

@Mapper(imports = { Instant.class, DateTimeFormatter.class })
@Configuration
public interface CommentMapper {

    CommentMapper INSTANCE = Mappers.getMapper(CommentMapper.class);

    @Mapping(source = "Id", target = "Id")
    @Mapping(source = "remark", target = "remark")
    Comment getComment(CommentDto postDto);

    @Mapping(source = "Id", target = "Id")
    @Mapping(source = "remark", target = "remark")
    CommentDto getCommentDto(Comment post);

}

我试图解决过去 6 小时的问题,但仍然不明白为什么它会显示以下错误

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project rest.api: Compilation failure: Compilation failure: 
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[22,17] No property named "Id" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[22,17] No property named "remark" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[26,20] No property named "Id" exists in source parameter(s). Did you mean "null"?
[ERROR] /D:/work/rest.api/src/main/java/com/rest/api/mapper/CommentMapper.java:[26,20] No property named "remark" exists in source parameter(s). Did you mean "null"?

任何有用的建议都将是解决此问题的真正希望。

其次,当需要所有 getter 和 setter 时,只有 @Data 可以吗?

出现错误的原因:“源参数中不存在名为“Id”的 属性。您的意思是“空”吗?”是因为 属性 个名字 Id 不存在。

您的 getter / setter 很可能看起来像 getId()setId(String Id).

这意味着从 MapStruct 的角度来看 属性 的名称是 id。 MapStruct 不查看私有字段,它检测基于 getters / setters 而不是字段的属性。

我知道我回答得太晚了,我对这个问题的回答是错误的,不幸的是,这个问题在搜索使用 lombok 和 mapstruct 引起的问题时出现(即,当你没有编写自己的 getter 而你没有混合 C# 风格和 K&R 风格的变量名 我找到的解决方案 here 是 re-order 注释处理器从 lombok 开始,然后是 mapstruct