Spring Boot项目中IntelliJ IDEA下Lombok报错的解决方法
解决IntelliJ IDEA中Lombok全参构造器无法识别的问题
问题描述
开发Spring Boot项目时,已在Task模型类中添加@Data、@AllArgsConstructor、@NoArgsConstructor注解,但IntelliJ IDEA编译时报错:
D:\Downloads\springwiththymefeaf\springwiththymefeaf\src\main\java\com\petproject\springwiththymefeaf\controller\TaskController.java:13:21 java: constructor Task in class com.petproject.springwiththymefeaf.model.Task cannot be applied to given types; required: no arguments found: int,java.lang.String,boolean reason: actual and formal argument lists differ in length
模型类代码:
package com.petproject.springwiththymefeaf.model; import lombok.*; @Data @AllArgsConstructor @NoArgsConstructor public class Task { private int id; private String title; private boolean completed; }
已尝试重新安装Lombok插件、启用注解处理、更新Lombok版本,但问题仍存在,且项目在VS Code中可正常运行。
解决方案
尝试以下步骤逐一排查:
1. 同步并刷新项目依赖
- 若使用Maven:点击右侧Maven面板的刷新按钮(圆形箭头),或在终端执行命令:
mvn clean install -U - 若使用Gradle:点击右侧Gradle面板的刷新按钮,或执行命令:
gradle clean build --refresh-dependencies
2. 确认编译器类型为javac
IntelliJ默认可能使用Eclipse编译器,Lombok对javac的支持更稳定:
- 打开
File > Settings > Build, Execution, Deployment > Compiler > Java Compiler - 将
Use compiler选项改为javac,点击Apply保存
3. 清除IDE缓存并重启
IDE缓存异常可能导致注解识别失败:
- 打开
File > Invalidate Caches... - 勾选
Clear file system cache and local history,点击Invalidate and Restart
4. 检查模块级注解处理设置
确保当前模块的注解处理已启用:
- 打开
File > Project Structure > Modules - 选择当前项目模块,切换到
Annotation Processors标签页 - 勾选
Enable annotation processing,点击OK保存
5. 明确指定Lombok依赖及注解处理器
确保构建文件中正确配置Lombok,包括注解处理器依赖:
Maven(pom.xml)
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <!-- 对应你的JDK版本 --> <target>17</target> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Gradle(build.gradle)
plugins { id 'java' id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok:1.18.30' annotationProcessor 'org.projectlombok:lombok:1.18.30' testCompileOnly 'org.projectlombok:lombok:1.18.30' testAnnotationProcessor 'org.projectlombok:lombok:1.18.30' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
内容的提问来源于stack exchange,提问作者Habibur Rahman
相关产品推荐
相关产品推荐

