Spring Boot中Lombok builder()返回空及IDE标红问题求助
Lombok @Builder 无法使用问题排查与解决
开发基于Spring Boot、JPA和Lombok的应用时,定义了包含Lombok注解的Author实体,但调用Author.builder()方法时,IntelliJ IDEA社区版中builder()部分标红,且运行时保存对象返回null。
相关代码
JpaApplication.java
package com.explorer.jpa; import com.explorer.jpa.models.Author; import com.explorer.jpa.repositories.AuthorRepository; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class JpaApplication { public static void main(String[] args) { SpringApplication.run(JpaApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(AuthorRepository repository) { return args -> { var author = Author.builder() // 此处在IntelliJ中显示为红色 .firstName("Johnny") .lastName("Deep") .age(35) .email("contact@disney.com") .build(); repository.save(author); }; } }
Author.java
package com.explorer.jpa.models; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; @Data @NoArgsConstructor @AllArgsConstructor @Builder @Entity public class Author { @Id @GeneratedValue private Integer id; @Column(name = "firstname", length = 50) private String firstName; @Column(name = "lastname") private String lastName; @Column(unique = true, nullable = false) private String email; private int age; @Column(updatable = false, nullable = false) private LocalDateTime createdAt; @Column(insertable = false) private LocalDateTime lastModified; }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.explorer</groupId> <artifactId>jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jpa</name> <description>Demo project for Spring Boot JPA</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <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> </project>
当前配置与排查记录
Lombok配置情况
- IntelliJ IDEA已安装并启用Lombok插件
- IntelliJ设置中已开启注解处理
已执行的排查操作
- 运行
mvn clean install - 在IntelliJ中重建项目
- 确认
Author类的Lombok注解正确 - 检查日志未发现Lombok或
builder()方法相关错误
额外信息
- IDE:IntelliJ IDEA Community Edition
- Java版本:17
- Spring Boot版本:3.3.1
解决方案
一、解决IntelliJ标红问题
- 清除IDE缓存并重启:
执行File > Invalidate Caches... > Invalidate and Restart,彻底刷新IDE缓存,解决静态代码识别问题。 - 重新导入Maven项目:
右键项目根目录,选择Maven > Reload Project,让IDE重新加载依赖和编译配置。 - 验证注解处理器配置:
打开File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors,确保:- 勾选
Enable annotation processing - 选中
Obtain processors from project classpath
- 勾选
二、解决运行时返回null问题
Author实体的createdAt字段标记为非空,但通过builder创建对象时未赋值,导致数据库插入失败,repository.save()返回null。可通过以下两种方式修复:
- 给
createdAt设置默认值:
在字段上添加@Builder.Default注解,自动赋值当前时间:@Builder.Default @Column(updatable = false, nullable = false) private LocalDateTime createdAt = LocalDateTime.now(); - builder创建时手动赋值:
调用builder时显式指定createdAt的值:var author = Author.builder() .firstName("Johnny") .lastName("Deep") .age(35) .email("contact@disney.com") .createdAt(LocalDateTime.now()) .build();
三、验证Maven编译
执行mvn clean compile,查看编译日志确认Lombok注解处理器正常运行,确保builder()方法已被正确生成。
内容的提问来源于stack exchange,提问作者Sri Murugan
相关产品推荐
相关产品推荐

