Feign common headers using RequestInterceptor

Carvia Tech | December 07, 2019 | 1 min read | 444 views


We will inject common headers to all requests in Feign using RequestInterceptor in Spring Boot application.

Gradle dependencies

We need to add OpenFeign dependency in our gradle project.

build.gradle
dependencies {
    implementation "org.springframework.boot:spring-boot-starter-web"
    implementation "org.springframework.cloud:spring-cloud-starter-openfeign"
}

RequestInterceptor Bean

Feign provides RequestInterceptor interface that can be used for adding/removing/mutating any part of the request.

RequestInterceptor.java
public interface RequestInterceptor {

    /**
    * Called for every request. Add data using methods on the supplied RequestTemplate.
    */
    void apply(RequestTemplate template);
}

All we need to do is to create a Bean of type RequestInterceptor inside a config class and provide that configuration to FeignClient.

FeignConfig.class
import feign.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

public class FeignConfig {

    @Bean
    public RequestInterceptor requestInterceptor() {
        return requestTemplate -> {
            requestTemplate.header("Content-Type", "application/json");
            requestTemplate.header("Accept", "application/json");
            requestTemplate.header("header_1", "value_1");
            requestTemplate.header("header_2", "value_2");
            requestTemplate.header("header_3", "value_3");
        };
    }
}

This will create a RequestInterceptor that will inject 5 headers to each request.

Now we will create a FeignClient using the above configuration:

Creating a Feign Client for stores-service
@FeignClient(value = "stores", configuration = {FeignConfig.class}) (1)
public interface StoreClient {

    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();
}
1 We have specified FeignConfig using configuration attribute of FeignClient annotation.

Running the application

Main application
@SpringBootApplication
@EnableFeignClients (1)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
1 Enabling the Feign Clients

Top articles in this category:
  1. Feign exception handling in Spring Cloud
  2. HTTP logging & connection timeout in Feign Clients with Spring Boot
  3. Retrofit vs Feign for Server Side
  4. How will you handle unresolved circular dependency in spring dependency injection?
  5. RestTemplate with Basic Authentication
  6. Send Gupshup SMS using Java API
  7. File Upload in Spring MVC using RestTemplate with ByteArrayResource & FileSystemResource


Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info