前言

最近在将原有的单体springboot项目搬迁至springcloud的时候出现了启动项目minio报错的问题。
Error creating bean with name ‘minioClient’ defined in class path resource [com/ruoyi/clockin_v2/config/MinioConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.minio.MinioClient]: Factory method ‘minioClient’ threw exception; nested exception is java.lang.NoSuchFieldError: Companion
在这里插入图片描述之前在启动单体springboot的时候并未出现这个问题,经过bean注册的过程发现是在使用okhttp3的HttpUrl.parse(url)的时候报的错
(通过代码的追踪发现是在执行endpoint的时候调用如下的HttpUrl.parse(endpoint);报错了)

 @Bean
 public MinioClient minioClient()
 {
     HttpUrl parse = HttpUrl.parse(url);
     System.out.println(parse.url());
     return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
 }
通过代码的追踪发现是在执行endpoint的时候调用如下的HttpUrl.parse(endpoint);报错了。所以可以确定是okhttp出现的问题

private HttpUrl getBaseUrl(String endpoint) {
   this.validateNotEmptyString(endpoint, "endpoint");
    HttpUrl url = HttpUrl.parse(endpoint);
    if (url == null) {
        this.validateHostnameOrIPAddress(endpoint);
        url = (new HttpUrl.Builder()).scheme("https").host(endpoint).build();
    } else {
        this.validateUrl(url);
    }

    return url;
}

然后通过maven依赖可以看到minio里面okhttp版本为4.8.1,但是实际的版本是使用的4.9.3,这是因为项目的springboot依赖的是okhttp的4.9.3的版本
minio的依赖版本
springboot的okhttp版本

通过以前正常启动项目的追踪,发现minio的正常启动的okhttp的版本为3.14.9
正常的okhtt版本为3.14.9
解决方式
将minio的依赖版本设置为3.14.9即可

 <!-- minio工具包 -->
 <dependency>
        <groupId>io.minio</groupId>
        <artifactId>minio</artifactId>
        <version>${minio.version}</version>
    </dependency>
    <!-- 解决minio使用okhttp高版本的时候注入bean实例报错的问题 -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.14.9</version>
        <scope>compile</scope>
    </dependency>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐