使用Lombok @RequiredArgsConstructor启动报错:NewsService未在默认构造器初始化
问题:Lombok @RequiredArgsConstructor生成构造器后启动报错:
java: variable NewsService not initialized in the default constructor 问题场景
Maven项目中使用Lombok的@RequiredArgsConstructor注解生成构造器,构建后target文件夹中能看到构造器已生成且项目打包成功,但启动时触发上述错误;手动编写构造器则可正常运行。
相关代码
控制器源码
@RestController @RequestMapping("/news") @RequiredArgsConstructor public class NewsController { private final NewsService newsService; @PostMapping("/create-news") public ResponseEntity<?> createNews(CreateNewsDto dto) { newsService.createNews(dto); return ResponseEntity.status(HttpStatus.CREATED).build(); } }
Target文件夹中生成的控制器代码
@RestController @RequestMapping({"/news"}) public class NewsController { private final NewsService newsService; @PostMapping({"/create-news"}) public ResponseEntity<?> createNews(CreateNewsDto dto) { this.newsService.createNews(dto); return ResponseEntity.status(HttpStatus.CREATED).build(); } @Generated public NewsController(final NewsService newsService) { this.newsService = newsService; } }
原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.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>news3</artifactId> <version>0.0.1-SNAPSHOT</version> <name>news3</name> <description>news3</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> <scope>provided</scope> <version>1.18.30</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>1.5.5.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.5.5.Final</version> </path> </annotationProcessorPaths> </configuration> </plugin> <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>
已尝试的操作
- 修改依赖版本
- 重新构建项目
- 在IntelliJ IDEA中启用注解处理
最终解决方法
- 为JpaRepository添加
@Repository注解(导入org.springframework.stereotype.Repository) - 更新pom.xml中的maven-compiler-plugin配置,添加lombok-mapstruct-binding依赖并指定插件版本:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.5.5.Final</version> </path> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> </path> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok-mapstruct-binding</artifactId> <version>0.2.0</version> </dependency> </annotationProcessorPaths> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin>
内容的提问来源于stack exchange,提问作者amirgilman
相关产品推荐
相关产品推荐

