Using @Headers with dynamic values in Feign client

2019-01-14 12:52发布

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

4条回答
手持菜刀,她持情操
2楼-- · 2019-01-14 13:18

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);
查看更多
SAY GOODBYE
3楼-- · 2019-01-14 13:19

I have this example, and I use @HeaderParam instead @RequestHeader :

import rx.Single;

import javax.ws.rs.Consumes;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;


@Consumes(MediaType.APPLICATION_JSON)
public interface  FeignRepository {

  @POST
  @Path("/Vehicles")
  Single<CarAddResponse> add(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader, VehicleDto vehicleDto);

}
查看更多
Root(大扎)
4楼-- · 2019-01-14 13:30

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);
}
查看更多
Lonely孤独者°
5楼-- · 2019-01-14 13:32

https://github.com/spring-cloud/spring-cloud-netflix/issues/760 https://github.com/OpenFeign/feign/#basics

17.3 Creating Feign Clients Manually
http://cloud.spring.io/spring-cloud-static/Dalston.SR4/single/spring-cloud.html#_creating_feign_clients_manually

pojo:

public class User...

service:

@RestController
public class HelloController ...
    public User getUser(@RequestParam("name") String name) {
        User user = new User();
        user.setName(name + "[result]");
        System.out.println("name: " + name);
        return user;
    }
    ...

client:

public interface HelloClient {
    @RequestLine("POST /getUser?name={name}")
    User getUser(@Param("name") String name);
}

use:

import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.Client;
public class Demo {
    private HelloClient helloClient;
    @Autowired
    public Demo(Decoder decoder, Encoder encoder, Client client) {
        this.userAnotherService = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                // for spring security
                .requestInterceptor(new BasicAuthRequestInterceptor("username", "password"))
                .target(UserAnotherService.class, "http://your-service-name");
    }
...
...method...
// output --> hello spring cloud![result]
System.out.println(helloClient.getUser("hello spring cloud!").getName());
...
查看更多
登录 后发表回答