我使用spring cloud&使用我的app假装客户端。我想在调用假客户端时将参数'accept-language'设置为标题。 我在[使用带有动态值的@Headers在Feign客户端+ 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) {
//...
}
我只能将“accept-language”的值作为MyDefaultFeignConfig配置第一次设置为@ Bean.How我可以设置从Gateway到假装客户端的“accept-language”值。请帮助我,想一想!任何建议都是感激和最好的问候!