SpringBoot中解决The Bean Validation API is on the classpath but no implementation could be found错误

在进行springboot第一个例子,hello world的输出过程中,遇到了许许多多的错误,下面把错误贴出来,希望大家遇到如下错误,可以快速解决。

@Controller
public class HelloWorldController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello world!";
    }
}

在运行上述代码过程中,出现了如下错误。

出现这个错误的原因是缺少Hibernate Validator依赖,于是在pom文件中添加如下依赖:

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

添加完成之后,运行报下错误:

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

报该错误的原因,是因为配置文件中缺少数据库相关配置,于是在application.properties中添加数据库相关配置:

#数据源
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driverClassName=org.postgresql.Driver

添加完数据库相关设置,又开始出现新的错误,如下:
在这里插入图片描述
这个错误出现的原因是数据库密码错误,但是数据库密码是正确的,在数据库软件dbeaver中都可以进行连接,产生原因是因为缺少注解数据库配置自动导入注解@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),在主程序类中加上如上注解,项目启动成功:
在这里插入图片描述
解决问题就跟套娃一样,一环套一环。
问题解决啦,hello world也显示出来了。真不容易!!!
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐