使用@FeignClient时如何传递多个RequestHeader
How to pass multiple RequestHeader when using @FeignClient
我需要使用 @FeignClient
传递多个请求 Headers
当它的一个 header 类型为 String 时,@RequestHeader 工作正常但有多个时我得到 RequestHeader.value() 是空的在参数 0 上,启动时出现 spring boot 错误。
@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作为参数传递,但它似乎不起作用
我需要使用自定义 RequestInterceptor
@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));
....
}
}
然后将配置class添加到你的feign client
@FeignClient(name="....",url="...",configuration=FeignCustomHeaderConfig.class)
我需要使用 @FeignClient
传递多个请求 Headers当它的一个 header 类型为 String 时,@RequestHeader 工作正常但有多个时我得到 RequestHeader.value() 是空的在参数 0 上,启动时出现 spring boot 错误。
@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作为参数传递,但它似乎不起作用
我需要使用自定义 RequestInterceptor
@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));
....
}
}
然后将配置class添加到你的feign client
@FeignClient(name="....",url="...",configuration=FeignCustomHeaderConfig.class)