Feign RequestInterceptor in Spring Boot

Carvia Tech | July 14, 2020 | 2 min read | 6,269 views

In this tutorial we will learn how to use Feign RequestInterceptor by inject common headers to all outgoing requests in a Spring Boot 2.x application.

Gradle dependencies

First of all, we will add OpenFeign dependency to our gradle project.

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

You can always head to https://start.spring.io/ for creating a Spring Boot starter project.

RequestInterceptor Bean

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

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.

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

public class FeignConfig {  (1)

    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");
1 Do not annotate this class with @Configuration annotation, otherwise this configuration will become global i.e. all FeignClients will inherit this config in that case.

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

Now we will create a FeignClient applying the above configuration (FeignConfig):

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.

Now all the requests made by StoreClient will include the common headers to outgoing Http requests.

Alternate configuration using application.properties

Optionally, we can configure our feign client using application.yml by specifying the request interceptors.

        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: full
          - com.foo.FooRequestInterceptor   (1)
1 This should be replaced by fully qualified name of the RequestInterceptor

Running the application

Main application
@EnableFeignClients (1)
public class Application {

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

1 Enabling the Feign Clients

There we go, using network logging we can see that additional headers are being applied by our newly created Feign RequestInterceptor.

You can also check Feign Client Logging Article

Top articles in this category:
  1. Feign Client Logging and connection timeout
  2. Feign exception handling in Spring Cloud
  3. Redis rate limiter in Spring Boot
  4. Setting a Random Port in Spring Boot Application at startup
  5. Testing web layer in Spring Boot using WebMvcTest
  6. Retrofit vs Feign for Server Side
  7. Basic Auth Security in Spring Boot 2

Find more on this topic:

Recommended books for interview preparation: