如何使用Feign客户端设置请求标头?
我们正在使用Spring Cloud框架开发一套微服务,我们要做的一件事情就是设置请求标头.我知道我可以将参数@RequestHeader
传递给Feign方法,但该值需要来自另一个bean.我不知道SPEL是否可用于Feign param值.
我以为对于大多数客户来说这是一个足够普遍的用例,因此会有一些示例,但是到目前为止我还没有找到任何示例.当然,我可以深入研究Spring课程代码,并尝试覆盖默认的Feign配置,但是如果我必须编写大量代码来实现这一点,这有点违反了声明式客户端的目的.
有什么想法吗?
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?
在使用RequestInterceptor之前,我已经完成了以下操作:
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: