Spring R2DBC连接PostgreSQL失败问题排查与解决求助
解决方案
1. 移除手动指定版本的r2dbc-postgresql依赖
你的pom.xml中手动添加并指定版本的r2dbc-postgresql,会与Spring Boot Starter管理的Reactor Core版本产生冲突——r2dbc-postgresql 1.0.4.RELEASE要求Reactor Core版本在3.4.x及以上,但当前Spring Boot Starter引入的Reactor版本可能低于此,最终导致NoSuchMethodError: Mono.singleOptional()(该方法是Reactor 3.4.0才新增的API)。
删除以下依赖:
<dependency> <groupId>org.postgresql</groupId> <artifactId>r2dbc-postgresql</artifactId> <version>1.0.4.RELEASE</version> </dependency>
2. 让Spring Boot统一管理依赖版本
spring-boot-starter-data-r2dbc已经集成了对R2DBC PostgreSQL驱动的依赖管理,无需手动引入。如果需要JDBC的PostgreSQL驱动(比如用于数据库初始化等场景),建议移除手动指定的版本,让Spring Boot统一管控:
修改后的postgresql依赖:
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
(添加runtime作用域,因为编译阶段不需要该依赖)
3. 验证配置并重启服务
确认application.yml中的R2DBC配置无误后,重新构建并启动应用,问题即可解决。
内容的提问来源于stack exchange,提问作者divad
相关产品推荐
相关产品推荐

