如何在Feign中使用自定义ApacheHttpClient?

问题描述 投票:0回答:1

我试图通过配置添加自定义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也以某种方式添加了Content-Length。

我需要设置哪个配置?

spring-cloud-feign netflix-feign feign
1个回答
0
投票

实际上,这是一个误解,上面的配置总是有效,我没有正确解析错误。返回的错误实际上来自api。

我所要做的就是正确指定errordecoder。

© www.soinside.com 2019 - 2024. All rights reserved.