sheetal
sheetal

Reputation: 3064

How to pass multiple RequestHeader when using @FeignClient

I need to pass multiple Request Headers using @FeignClient

When its one header of type String the @RequestHeader works fine but with multiple I get RequestHeader.value() was empty on parameter 0, while starting the spring boot error .

           @RequestMapping(value="/*********employees", method= RequestMethod.GET , consumes = MediaType.APPLICATION_JSON_VALUE)
EmployeeData fetchWorkdayEmployeess(@RequestHeader Map<String, Object> headers);

as well as I tried using @HeaderMap

    @RequestMapping(value="/*********employees", method= RequestMethod.GET , consumes = MediaType.APPLICATION_JSON_VALUE)
EmployeeData fetchWorkdayEmployeess(@HeaderMap Map<String, Object> headers);

I also tried passing multiple @RequestHeaders as parameters but it doesn't seem to work

Upvotes: 0

Views: 3441

Answers (1)

sheetal
sheetal

Reputation: 3064

I needed to use a custom 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));
            ....
        }
    }

And then add the configuration class to your feign client

@FeignClient(name="....",url="...",configuration=FeignCustomHeaderConfig.class)

Reference link here :

Upvotes: 2

Related Questions