我需要使用@FeignClient传递多个请求标头
当它的一个字符串类型的标头时,@RequestHeader工作正常,但对于多个,我得到RequestHeader.value(( 在参数 0 上为空,同时启动 spring 引导错误。
@RequestMapping(value="/*********employees", method= RequestMethod.GET , consumes = MediaType.APPLICATION_JSON_VALUE)
EmployeeData fetchWorkdayEmployeess(@RequestHeader Map<String, Object> headers);
以及我尝试使用@HeaderMap
@RequestMapping(value="/*********employees", method= RequestMethod.GET , consumes = MediaType.APPLICATION_JSON_VALUE)
EmployeeData fetchWorkdayEmployeess(@HeaderMap Map<String, Object> headers);
我还尝试将多个@RequestHeaders作为参数传递,但它似乎不起作用
我需要使用自定义的请求拦截器
@Configuration
class FeignCustomHeaderConfig {
@Bean
public CSODHeaderAuthRequestInterceptor basicAuthRequestInterceptor() {
try {
return new HeaderAuthRequestInterceptor(token_map);
} catch (Exception e) {
log.error(e.getLocalizedMessage());
}
return new CSODHeaderAuthRequestInterceptor(null);
}
class HeaderAuthRequestInterceptor implements RequestInterceptor {
//Expensive OAuth2 flow logic
private HashMap<String, String> tokenMap;
public HeaderAuthRequestInterceptor(HashMap<String, String> tokenMap) {
this.tokenMap = tokenMap;
}
@Override
public void apply(RequestTemplate requestTemplate) {
if(tokenMap == null)
return;
requestTemplate.header(key1, tokenMap.get(key1));
requestTemplate.header(key2, tokenMap.get(key2));
....
}
}
然后将配置类添加到您的假客户端
@FeignClient(name="....",url="...",configuration=FeignCustomHeaderConfig.class)
参考链接在这里 :