SpringBoot项目报错:The Bean Validation API is on the classpath but no implementation could be found

构建简单的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中添加

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.0.Final</version>
        </dependency>

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

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);
    }
}

猜你喜欢

转载自blog.csdn.net/zixiao217/article/details/81011085