使用influx-client-reactive与Quarkus时出现NoSuchFieldError: Companion错误
Quarkus集成InfluxDB Reactive客户端的OkHttp依赖冲突问题及解决
问题现象
在Quarkus项目中执行以下代码实例化InfluxDB Reactive客户端时,会触发依赖冲突类错误:
InfluxDBClientReactive influxDBClient = InfluxDBClientReactiveFactory.create( influxConf.url(), influxConf.username(), influxConf.password().toCharArray());
两种错误场景
- 未排除Quarkus BOM中的OkHttp依赖:
Quarkus会强制引入3.x.x版本的OkHttp,此时会触发方法不存在错误:
java.lang.NoSuchMethodError: 'okhttp3.RequestBody okhttp3.RequestBody.create(java.lang.String, okhttp3.MediaType)'
- 已排除Quarkus BOM中的OkHttp依赖:
实例化客户端时会触发字段不存在错误,完整堆栈如下:
java.lang.NoSuchFieldError: Companion at okhttp3.internal.Util.<clinit>(Util.kt:70) at okhttp3.HttpUrl$Builder.parse$okhttp(HttpUrl.kt:1239) at okhttp3.HttpUrl$Companion.get(HttpUrl.kt:1634) at okhttp3.HttpUrl$Companion.parse(HttpUrl.kt:1643) at okhttp3.HttpUrl.parse(HttpUrl.kt) at com.influxdb.client.InfluxDBClientOptions$Builder$ParsedUrl.<init>(InfluxDBClientOptions.java:689) at com.influxdb.client.InfluxDBClientOptions$Builder$ParsedUrl.<init>(InfluxDBClientOptions.java:681) at com.influxdb.client.InfluxDBClientOptions$Builder.connectionString(InfluxDBClientOptions.java:504) at com.influxdb.client.InfluxDBClientOptions$Builder.url(InfluxDBClientOptions.java:288) at com.influxdb.client.reactive.InfluxDBClientReactiveFactory.create(InfluxDBClientReactiveFactory.java:105)
当前尝试的依赖配置
已从Quarkus BOM中排除OkHttp依赖,并引入InfluxDB Reactive客户端6.4.0版本:
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}") { exclude group: "com.squareup.okhttp3", module: "okhttp" } implementation "com.influxdb:influxdb-client-reactive:6.4.0"
核心原因
InfluxDB Client Reactive 6.4.0依赖的OkHttp版本为4.x+,与Quarkus BOM强制的3.x版本存在API不兼容:
- OkHttp 4.x基于Kotlin重写,新增了
Companion静态字段,3.x版本无此结构,导致NoSuchFieldError - OkHttp 3.x的
RequestBody.create方法签名在4.x中已被废弃并修改,导致NoSuchMethodError
解决方案
- 显式声明兼容的OkHttp版本:
排除Quarkus BOM中的OkHttp及关联的Okio依赖后,手动引入与InfluxDB客户端匹配的OkHttp版本(InfluxDB 6.4.0适配OkHttp 4.10.0及以上):
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}") { exclude group: "com.squareup.okhttp3", module: "okhttp" exclude group: "com.squareup.okhttp3", module: "okio" } implementation "com.squareup.okhttp3:okhttp:4.10.0" implementation "com.influxdb:influxdb-client-reactive:6.4.0"
- 验证依赖树:
执行Gradle命令检查依赖树,确保OkHttp版本统一无冲突:
./gradlew dependencies --configuration compileClasspath | grep okhttp
确认输出中仅存在4.x版本的OkHttp,无3.x版本残留。
- 改用Quarkus官方InfluxDB扩展:
如果手动处理依赖仍有问题,可直接使用Quarkus官方提供的InfluxDB扩展,它会自动处理依赖兼容:
implementation 'io.quarkus:quarkus-influxdb'
之后通过Quarkus的依赖注入方式获取客户端,无需手动调用InfluxDBClientReactiveFactory.create。
内容的提问来源于stack exchange,提问作者Rin malavi
相关产品推荐
相关产品推荐

