scx12333 2019-08-18 11:22 采纳率: 100%
浏览 4474
已采纳

openfeign里面配置httpclient出错。

因为现在需要使用get方式传递对象参数,所以尝试在openfeign里面配置httpclient。
配置代码如下:
在yml文件里面增加了配置信息

feign:
  httpclient:
    enabled: true

在pom.xml文件中引入的依赖:

       <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!-- 使用Apache HttpClient替换Feign原生httpclient -->
        <dependency>
            <groupId>com.netflix.feign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <version>8.16.1</version>
        </dependency>

配置好以后尝试使用get方式传递对象参数。
服务提供端代码:

 @GetMapping(value = "testAddRole1",consumes = "application/json")
    public JsonResult addTest1(@RequestBody Role role){
        roleService.addRole(role);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS);
    }

服务调用接口为:

@GetMapping(value = "/testAddRole1",consumes = "application/json")
    JsonResult testAddRole1(@RequestBody Role role);

现在在尝试调用接口后发现服务提供方可以接受到参数并且能够写到数据库中,但是在服务调用方拿不到正确的返回值并且会报错,错误如下:

Caused by: java.lang.NoSuchMethodError: feign.Response.create(ILjava/lang/String;Ljava/util/Map;Lfeign/Response$Body;)Lfeign/Response;

查看源码得知,openfeign在接受返回值时调用的不是httpclient的feign-core包的代码而是调用的本身的feign-core的代码,而本身的feign-core包中的Response类没有create方法。
得知原因后我将httpclient的依赖在pom文件中的位置上移,使openfeign优先调用httpclient的feign-core包的代码,结果在启动项目时就报错,报错信息为:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test1Controller' defined in file [D:\workspace\basic\openfeign\target\classes\cn\cloudscope\openfeignTest\Test1\controller\Test1Controller.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.cloudscope.openfeignTest.Test1.service.Test1Service': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignRetryer' defined in org.springframework.cloud.openfeign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [feign.Retryer]: Factory method 'feignRetryer' threw exception; nested exception is java.lang.NoSuchFieldError: NEVER_RETRY

查看原因得知是两个feign-core包中的Retryer接口不一致导致的,求来个大神帮忙解决一下,或者怎么调整jar包的版本去解决这个问题。openfeign的feign-core版本为10.1.0 httpclient的版本为8.16.1

  • 写回答

2条回答 默认 最新

  • c小小菜 2019-08-24 15:05
    关注

    我也遇到了这个问题,把你的feign-httpclient依赖换成这个

    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
        <version>10.1.0</version>
    </dependency>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵