首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Data Startup异常处理

Spring Data Startup异常处理
EN

Stack Overflow用户
提问于 2020-07-30 03:51:23
回答 1查看 132关注 0票数 0

在启动过程中连接数据库失败后,如何捕捉Spring抛出的异常?我想让错误消息变得用户友好。

例如,如果密码不正确,则会抛出以下异常:

代码语言:javascript
复制
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "cikfedlekqbuk"
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:525) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:146) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197) ~[postgresql-42.2.14.jar:42.2.14]
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar:42.2.14]
...

或者,如果格式不正确,则会抛出以下异常:

代码语言:javascript
复制
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://url:port
... Stack trace
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://url:port
... Stack trace
Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://url:port
... Stack trace

我遇到的两种可能的解决方案是使用@ExceptionHandler或使用AOP。然而,这两种方法都失败了,因为@ExceptionHandler是特定于MVC的,而AOP无法识别这些方法。下面是我尝试过的一个切入点的例子:

代码语言:javascript
复制
@AfterThrowing(value="execution(* org.springframework.boot.autoconfigure.orm.jpa.*())", throwing="e")

我也尝试过com.zaxxer.hikari.util.DriverDataSource和其他一些工具。

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2020-07-30 22:44:36

检查您的application.properties:

代码语言:javascript
复制
spring.datasource.url = jdbc:postgresql://localhost:5432/database_name
spring.datasource.username = postgres
spring.datasource.password = postgres
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63161434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档