Headers与动态值一起使用

Headers与动态值一起使用

本文介绍了在Feign客户端+ Spring Cloud(Brixton RC2)中将@Headers与动态值一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to set dynamic values to a header ?

@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的实现会添加标头,但无法动态设置标头值

Registering an implementation of RequestInterceptor adds the header but there is no way of setting the header value dynamically

@Bean
    public RequestInterceptor requestInterceptor() {

        return new RequestInterceptor() {

            @Override
            public void apply(RequestTemplate template) {

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

我在github上发现了以下问题,其中一个注释者( lpborges )试图使用@RequestMapping注释中的标头来做类似的事情.

I found the following issue on github and one of the commenters (lpborges) was trying to do something similar using headers in @RequestMapping annotation.

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

亲切的问候

推荐答案

解决方案是使用@RequestHeader批注而不是伪装的特定批注

The solution is to use @RequestHeader annotation instead of feign specific annotations

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

这篇关于在Feign客户端+ Spring Cloud(Brixton RC2)中将@Headers与动态值一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:50