【问题标题】:How to use custom ApacheHttpClient with Feign?如何将自定义 ApacheHttpClient 与 Feign 一起使用?
【发布时间】:2019-02-27 18:49:47
【问题描述】:

我尝试通过配置添加自定义 HttpClient:

 @Bean
 public CloseableHttpClient httpClient() {
    RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(15000)
                .setConnectionRequestTimeout(15000)
                .build();

    Header header = new BasicHeader("Test", "Test");
    Collection<Header> headers =Arrays.asList(header);        
    return HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setDefaultHeaders(headers)
                .build();
 }

但是,我自定义添加的默认标头仍然没有出现在请求中。

我的 Feign 客户端界面如下所示:

@FeignClient(name = "example", 
             url = "${client.example.api}", 
             decode404 = false, 
             configuration = FeignClientConfiguration.class)
public interface ExampleFeignProxy{

    @PostMapping(path = "/create")
    @Headers("Content-Type: application/json")
    String Create(
            @RequestBody ExampleDTO exampleDto,
            @RequestHeader("access-token") String token);
}

但是当我向 Create 方法发出请求时,请求失败,当我检查 configuration.errordecoder 内部时,它显示 feign 也在向请求中添加一个额外的标头 Content-Length。 如何从我的 feign 客户端中的所有方法中删除默认标头?

为了清楚起见 - 如上所示,请求对象上应该只存在两个标头

  • 内容类型

  • 访问令牌

但 Feign 也以某种方式添加了 Content-Length。

我需要设置什么配置吗?

【问题讨论】:

    标签: spring-cloud-feign netflix-feign feign


    【解决方案1】:

    其实这是个误会,上面的配置一直都在工作,我没有正确解析错误。返回的错误实际上来自 api。

    我所要做的就是正确指定错误解码器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2012-04-01
      相关资源
      最近更新 更多