Feign设置动态header

Feign动态设置Header

https://www.jianshu.com/p/4d3cede9bc88

Feign设置全局header

Feign 调用接口是经常会忘header信息,再次介绍两种忘header里面添加信息的方式:
1.通过直接在请求上,或者在类上添加Headers的注解

@Headers({"Content-Type: application/json","Accept: application/json",Accept {contentType}})
@PostMapping(value = "/card-blank/batch-create")
Response batchCreateCard(@RequestBody CreateCardBlankDTO condition,@Param("contentType") String type);

使用{contentType} 可以传递动态header属性
2. 通过实现RequestInterceptor接口,完成对所有的Feign请求,设置Header

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

@Component
public class FeignInterceptor implements RequestInterceptor{

    public void apply(RequestTemplate requestTemplate){
        requestTemplate.header("hotelId", "111111");
    }
}

这种方式也可以设置请求的其他属性,很灵活的一种方式:

Feign + ThreadLocal 设置动态Header

https://github.com/yinjihuan/spring-cloud/blob/master/fangjia-api-client/src/main/java/com/fangjia/api/client/config/FeignBasicAuthRequestInterceptor.java
ThreadLocal 设置值
https://github.com/yinjihuan/spring-cloud/blob/master/fangjia-common/src/main/java/com/fangjia/common/filter/HttpHeaderParamFilter.java

Spring Cloud之Feign 转发请求头(header参数)
https://www.cnblogs.com/tulu/p/9765675.html

你可能感兴趣的:(springcloud2)