SpringBoot项目使用了@EnableAutoConfiguration 注解报错:The Bean Validation API is on the classpath but no impl

淩亂°似流年 2022-05-20 04:12 470阅读 0赞

构建简单的SpringBoot项目时,在启动项目的时候发现如下错误:
Description:
The Bean Validation API is on the classpath but no implementation could be found
Action:
Add an implementation, such as Hibernate Validator, to the classpath

意思是Bean的校验API在classpath中没有找到实现的类。建议添加一个Validation 的实现,比如在classpath下添加一个Hibernate Validator的实现。 因为使用了 @EnableAutoConfiguration 注解,Spring则可能会尝试寻找一个关于Java specification for Bean Validation的实现 (更多详情请见: spring validation).

解决方案: 添加一个Hibernate Validator依赖实现。
在POM中添加

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-validator</artifactId>
  4. <version>5.3.0.Final</version>
  5. </dependency>

运行的示例代码程序如下:

  1. package com.example.myproject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello world! * */ @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main( String[] args ) { SpringApplication.run(Example.class, args); } }

发表评论

表情:
评论列表 (有 0 条评论,470人围观)

还没有评论,来说两句吧...

相关阅读