How to Set Request Headers Using a Feign Client?

两盒软妹~` 提交于 2020-01-01 03:29:12

问题


We are developing a suite of Microservices using Spring Cloud framework and one of the the things that we need to do is to set request headers. I know I can pass a parameter @RequestHeader to a Feign method but the value needs to come from another bean. I don't know if SPEL can be used for a Feign param value. I was thinking that this is a common enough use case for most clients so there'd be some examples, but so far I've not found any. Of course I can dig through the Spring course code and try to override the default Feign configuration but it kinda defeats the purpose of a declarative client if I've to write a lot of code to achieve this. Any thoughts?


回答1:


I have done this before using a RequestInterceptor as follows:

@Component
public class MyRequestInterceptor implements RequestInterceptor {
  @Override
  public void apply(RequestTemplate template) {
    template.headers(getHeadersFromWherever());
  }
}

You can find some more useful information here:

https://github.com/Netflix/feign#user-content-setting-headers-per-target



来源:https://stackoverflow.com/questions/37289634/how-to-set-request-headers-using-a-feign-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!