Using @Headers with dynamic values in Feign client + Spring Cloud (Brixton RC2)

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);
    }

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");
            }
        };
    } 

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

Kind Regards


Solution 1:

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);
}

Solution 2:

The @RequestHeader did not work for me. What did work was:

@Headers("X-Auth-Token: {access_token}")
@RequestLine("GET /orders/{id}")
Order get(@Param("id") String id, @Param("access_token") String accessToken);

Solution 3:

@HeaderMap,@Header and @Param didn't worked for me, below is the solution to use @RequestHeader when there are multiple header parameters to pass using FeignClient

@PostMapping("/api/channelUpdate")
EmployeeDTO updateRecord(
      @RequestHeader Map<String, String> headerMap,
      @RequestBody RequestDTO request);

code to call the proxy is as below:

Map<String, String> headers = new HashMap<>();
headers.put("channelID", "NET");
headers.put("msgUID", "1234567889");
ResponseDTO response = proxy.updateRecord(headers,requestDTO.getTxnRequest());