0

I use spring cloud & feign client with my app.And I want to set the param 'accept-language' to headers when call feign clients. I found the similar questions at [Using @Headers with dynamic values in Feign client + Spring Cloud (Brixton RC2)

Ask]1,but I don't know how to set header param.Here is my code: I set MyDefaultFeignConfig at app.java @EnableFeignClients(basePackages = {defaultConfiguration = MyDefaultFeignConfig.class)

And 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()
    );

}
}

My Gateway controller is :

@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();
}

My feign clients Controller is:

public Message unifiyOrderInit(@RequestHeader("accept-language") String language) {
    //...
}

And I can only get the value of "accept-language" as MyDefaultFeignConfig config the first time set @Bean.How can I set the value of "accept-language" from Gateway to feign client.Please help me,thinks! Any suggestions are grateful and best regards!

Community
  • 1
  • 1
Wentao Wan
  • 111
  • 1
  • 2
  • 8
  • You should be able to use `@RequestHeader("accept-language") String language` on the feign client itself. BTW, your question title is really vague. – spencergibb Apr 12 '17 at 18:42
  • thanks,spencergribb! Are you mean to add @RequestHeader("accept-language") String language in every feign client method?Is it so messy? – Wentao Wan Apr 13 '17 at 01:14
  • You can also use a `RequestInterceptor`. – spencergibb Apr 14 '17 at 16:36
  • Could you please explain it clearer?As I know,i can only use `RequestInterceptor` to set header when app start to inject beans.How can i set it dynamically in methods.Thank you for your reply. – Wentao Wan Apr 17 '17 at 01:59
  • nope, request interceptors can dynamically add headers to each request. http://stackoverflow.com/questions/37289634/how-to-set-request-headers-using-a-feign-client/37324653#37324653 – spencergibb Apr 17 '17 at 17:44

0 Answers0