Spring Boot 3升级后R2dbc DatabaseClient参数绑定报错排查
Spring Boot 3.0.5升级后DatabaseClient绑定参数报错:Cannot encode parameter of type io.r2dbc.spi.Parameters$InParameter
问题原因
核心问题是R2DBC驱动与Spring Boot 3.x版本不兼容:
- Spring Boot 3.0.x对应的Spring Data R2DBC 3.0.x依赖的是R2DBC 1.x规范,但你使用的
r2dbc-postgresql是0.8.13.RELEASE(属于0.8系列,对应旧版R2DBC 0.9规范)。 - 新旧版本参数绑定逻辑不兼容:R2DBC 1.x中参数处理不再需要手动包装为
InParameter,但旧驱动会错误地将传入参数包装成该类型,导致编码失败。 - 同时
r2dbc-pool的手动指定版本(1.0.0.RELEASE)与旧postgresql驱动搭配,会进一步加剧版本冲突。
解决方案
1. 统一R2DBC相关依赖版本
修改pom.xml,移除手动指定的R2DBC驱动版本,让Spring Boot的依赖管理自动匹配兼容版本:
<!-- 移除r2dbc-postgresql和r2dbc-pool的手动版本号 --> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-pool</artifactId> <scope>runtime</scope> </dependency>
Spring Boot 3.0.5会自动引入r2dbc-postgresql:1.0.2.RELEASE和r2dbc-pool:1.0.1.RELEASE,完全匹配R2DBC 1.x规范。
2. 确保Spring Cloud版本兼容
Spring Boot 3.0.x对应Spring Cloud 2022.0.x(Kilburn),检查你的spring-cloud.version是否设置正确:
<properties> <spring-cloud.version>2022.0.3</spring-cloud.version> </properties>
避免Spring Cloud依赖与Spring Boot版本不匹配引发的间接冲突。
3. 检查参数类型
确保parse(s)方法返回的a是原始数据类型(如String、Integer等),而非InParameter或其他包装类型。如果parse逻辑中有旧版R2DBC的参数包装代码,需移除:
// 错误示例(旧版逻辑) // Parameters.in(a) // 正确:直接传入原始类型 .bind("a", a)
验证修改
修改依赖后,执行mvn clean install -U清理Maven缓存并重新构建,重新运行应用即可验证参数绑定是否正常工作。
内容的提问来源于stack exchange,提问作者ccc134
相关产品推荐
相关产品推荐

