我在我的应用程序中使用 spring cloud 和 feign 客户端。我想在调用 feign 客户端时将参数“accept-language”设置为标题。我在 [Using @Headers with dynamic values in Feign client + Spring Cloud (Brixton RC2)
问] 1,但我不知道如何设置标头参数。这是我的代码:我在 app.java 中设置了 MyDefaultFeignConfig
@EnableFeignClients(basePackages = {defaultConfiguration = MyDefaultFeignConfig.class)
和 MyDefaultFeignConfig.java :
@Configuration
public class MyDefaultFeignConfig {
private String requestLanguage = "zh";
@Bean
RequestInterceptor feignRequestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header("accept-language", requestLanguage);
}
};
}
//doesn't work
public static void updateBean(String requestLanguage) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyDefaultFeignConfig.class);
try {
System.out.println(applicationContext.getBean("feignRequestInterceptor"));
} catch (NoSuchBeanDefinitionException e) {
System.out.println("Bean not found");
}
BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
beanFactory.registerBeanDefinition("feignRequestInterceptor",
BeanDefinitionBuilder.genericBeanDefinition(String.class)
.addConstructorArgValue(new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
template.header("accept-language", requestLanguage);
}
})
.getBeanDefinition()
);
}
}
我的网关控制器是:
@Autowired
private LeaseOrderRemoteService leaseOrderRemoteService;
@RequestMapping(value = "/Discovery/order/unifiyInit", method = RequestMethod.GET)
public Message unifiyOrderInit(@RequestHeader("accept-language") String language) {
MyDefaultFeignConfig.updateBean(language);
return leaseOrderRemoteService.unifiyOrderInit();
}
我的假客户控制器是:
public Message unifiyOrderInit(@RequestHeader("accept-language") String language) {
//...
}
而且我只能在第一次设置@Bean时将“accept-language”的值作为MyDefaultFeignConfig config。如何将“accept-language”的值从Gateway设置为feign client。请帮助我,想想!任何建议都表示感谢和最好的问候!