You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目Lombok注解报错:找不到setName等Setter方法

解决Lombok @Setter注解无法生成setter方法的问题

针对你遇到的cannot find symbol: method setName(java.lang.String)错误,以下是几个可行的解决方案:

1. 修正Maven注解处理器配置

你的pom.xml中,annotationProcessorPaths配置没有放在maven-compiler-plugin的范围内,导致编译器无法识别Lombok的注解处理器。正确的配置应该将其嵌套在编译器插件中:

<build>
    <plugins>
        <!-- 配置maven编译器插件,指定Lombok作为注解处理器 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version> <!-- 与依赖版本保持一致 -->
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

        <!-- 保留Spring Boot Maven插件的配置 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

同时建议在依赖中明确指定Lombok版本,避免版本兼容问题:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <optional>true</optional>
</dependency>

2. 清理IDE缓存并重新构建

即使开启了注解处理,IDE缓存也可能导致代码识别异常:

  • 对于IntelliJ IDEA:执行File -> Invalidate Caches...,勾选Clear file system cache and local history后重启IDE
  • 执行Maven命令清理并重新构建:mvn clean install

3. 调整实体类字段的访问修饰符

虽然Lombok理论上会为public字段生成setter/getter,但将字段改为private更符合Java封装原则,也能避免潜在的注解生成异常。修改后的User类示例:

@Entity
@Table(name = "users")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @NotEmpty
    @Column(nullable = false)
    private String name;

    @NotEmpty
    @Email
    @Column(unique = true, nullable = false)
    private String email;

    @NotEmpty
    @Size(min = 8)
    @Column(nullable = false)
    private String password;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private UserRoles role;

    @Column(name = "created_at")
    private Timestamp createdAt;
}

内容的提问来源于stack exchange,提问作者André

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:53:27