在 Feign 客户端 + Spring Cloud (Brixton RC2) 中使用带有动态值的 @Headers

2024-03-11

是否可以为标头设置动态值?

@FeignClient(name="Simple-Gateway")
interface GatewayClient {
    @Headers("X-Auth-Token: {token}")
    @RequestMapping(method = RequestMethod.GET, value = "/gateway/test")
        String getSessionId(@Param("token") String token);
    }

注册 RequestInterceptor 的实现会添加标头,但无法动态设置标头值

@Bean
    public RequestInterceptor requestInterceptor() {

        return new RequestInterceptor() {

            @Override
            public void apply(RequestTemplate template) {

                template.header("X-Auth-Token", "some_token");
            }
        };
    } 

我在 github 和评论者之一上发现了以下问题(lpborges)试图使用标题做类似的事情@RequestMapping注解。

https://github.com/spring-cloud/spring-cloud-netflix/issues/288 https://github.com/spring-cloud/spring-cloud-netflix/issues/288

亲切的问候


解决方案是使用@RequestHeader注解而不是feign特定的注解

@FeignClient(name="Simple-Gateway")
interface GatewayClient {    
    @RequestMapping(method = RequestMethod.GET, value = "/gateway/test")
    String getSessionId(@RequestHeader("X-Auth-Token") String token);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Feign 客户端 + Spring Cloud (Brixton RC2) 中使用带有动态值的 @Headers 的相关文章

随机推荐