如何使用Feign客户端设置请求标头? [英] How to Set Request Headers Using a Feign Client?

查看:295
本文介绍了如何使用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:

https://github.com/Netflix/feign#每个目标的用户内容设置标题

这篇关于如何使用Feign客户端设置请求标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆