Spring应用启动报错缺少Bean Validation实现类如何排查解决
问题根因
这个报错是因为项目类路径中只引入了Bean Validation的API接口包,没有引入对应的实现类。从Spring Boot 2.3版本开始,spring-boot-starter-web等Web场景启动器不再默认传递引入Bean Validation的实现依赖,因此会触发该错误。
解决方法
直接引入Spring官方提供的spring-boot-starter-validation场景启动器即可,它会自动引入和当前Spring Boot版本完全适配的Hibernate Validator实现,不需要手动指定版本,避免版本兼容问题。
根据你使用的构建工具选择对应配置:
Maven
在项目pom.xml的<dependencies>标签内添加以下内容:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
Gradle (Groovy DSL)
在项目build.gradle的dependencies块内添加以下内容:
implementation 'org.springframework.boot:spring-boot-starter-validation'
Gradle (Kotlin DSL)
在项目build.gradle.kts的dependencies块内添加以下内容:
implementation("org.springframework.boot:spring-boot-starter-validation")
排查点
如果添加依赖后仍然报错,优先检查两个点:
- 是否在其他依赖配置中通过
<exclusions>/exclude排除了Hibernate Validator相关的包 - 是否手动在依赖中指定了和当前Spring Boot版本不匹配的Hibernate Validator或Bean Validation版本
添加完依赖记得刷新构建工具的依赖,重新启动应用即可生效。
内容的提问来源于stack exchange,提问作者user7072363
相关产品推荐
相关产品推荐

