IntelliJ运行Spring Boot应用时无法识别Lombok生成的setter方法
IntelliJ运行Spring Boot应用时Lombok Setter找不到,但Maven构建成功的解决方法
运行Spring Boot应用时,IntelliJ抛出Lombok setter相关错误(如java: cannot find symbol symbol: method setId(java.lang.String)),但项目结构无编译问题,且Maven构建可成功完成。
Build配置
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-image</goal> </goals> </execution> </executions> <configuration> <skip>true</skip> <image> <name>restaurant-image</name> </image> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> <layers> <enabled>true</enabled> </layers> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven-surefire-plugin.version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.1</version> <configuration> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>${org.projectlombok.lombok-mapstruct-binding.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Maven构建警告(构建成功)
WARNING: A Java agent has been loaded dynamically (C:\Users\mazin\.m2\repository\net\bytebuddy\byte-buddy-agent\1.14.12\byte-buddy-agent-1.14.12.jar) WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information WARNING: Dynamic loading of agents will be disallowed by default in a future release OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended [WARNING] Overwriting artifact's file from C:\Users\mazin\Downloads\demo\restaurant\target\restaurant-0.0.1-SNAPSHOT.jar to C:\Users\mazin\Downloads\demo\restaurant\target\classes
运行时错误
java: cannot find symbol symbol: method setId(java.lang.String)
解决步骤
1. 确认IntelliJ的Lombok插件已启用
- 打开IntelliJ设置(
File > Settings),搜索Plugins - 找到Lombok插件,确保处于启用状态,未启用则安装后重启IDE
2. 开启注解处理器支持
- 进入
Settings > Build, Execution, Deployment > Compiler > Annotation Processors - 勾选
Enable annotation processing选项,点击Apply保存
3. 刷新Maven依赖并重新构建
- 在IntelliJ右侧Maven面板,点击
Reload All Maven Projects按钮 - 执行
clean和compile命令,确保Lombok生成的字节码正常生成
4. 调整Spring Boot插件的exclude配置
你的Spring Boot Maven插件中排除了Lombok:
<excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes>
虽然Maven构建能成功,但IDE运行时需要依赖Lombok解析注解,可尝试移除该exclude配置。
5. 清除IDE缓存并重启
- 执行
File > Invalidate Caches...,选择Invalidate and Restart - 解决可能存在的缓存不一致问题
内容的提问来源于stack exchange,提问作者Mazin Ismail
相关产品推荐
相关产品推荐

