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

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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题