Feign设置header

Feign 调用接口是经常会忘header信息,再次介绍两种忘header里面添加信息的方式:
1.通过直接在请求上,或者在类上添加Headers的注解

@Headers({"Content-Type: application/json","Accept: application/json",Accept {contentType}})
@PostMapping(value = "/card-blank/batch-create")
Response batchCreateCard(@RequestBody CreateCardBlankDTO condition,@Param("contentType") String type);

使用{contentType} 可以传递动态header属性
2. 通过实现RequestInterceptor接口,完成对所有的Feign请求,设置Header

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

@Component
public class FeignInterceptor implements RequestInterceptor{

    public void apply(RequestTemplate requestTemplate){
        requestTemplate.header("hotelId", "111111");
    }
}

这种方式也可以设置请求的其他属性,很灵活的一种方式:

参考文档:
https://my.oschina.net/aulbrother/blog/1610011

猜你喜欢

转载自blog.csdn.net/fzy629442466/article/details/89154539