Customizing each request with Spring Cloud Feign

Recently I wrote an article  how to explicitly pass headers via Feign clients. Now I’ll show you how to customize each request sent via Feign clients, like adding headers to all of them without the need to explicitly define them.

Let’s see the previous example, a translator and a test application.

The client calls the test-app and then it calls to the translator and the translator does the job according to the original  Accept-Language  header sent by the client.

Let’s see the translator application’s code, it has a single controller:

Simple. Okay let’s see the test-app’s code. The controller which the client can trigger:

TranslationClient  is the Feign client:

As you can see there is no RequestHeader  parameter defined for the method so how can we pass the header information?

Feign has a so called RequestInterceptor which will be applied for all the requests sent by Feign clients. The only thing that’s necessary is to define a custom RequestInterceptor class and register it as a Spring bean. Let’s see how it looks:

So the important thing here is to first get the header information from the incoming HTTP request. This can be done using the RequestContextHolder#getRequestAttributes which is holding the current request information. After we got the original request, extracting the Accept-Language  header is not a complicated thing to do. Then simply using the RequestTemplate , the header can be set for the Feign requests.

Marking the class with @Component  will define it as a Spring bean and we are done.

Now if we try to invoke the test-app from your favorite REST client with the Accept-Language  header with value de-DE , you will see the german text coming back in the response.

Using this example, you can customize your requests in any way you like.

The code can be found on my GitHub page. If you are interested in more topics, make sure you follow me on Twitter and shout out if you have questions.

One Reply to “Customizing each request with Spring Cloud Feign”

  1. I want to thank you for your blog post. I have an API Gateway, which uses Feign and Zuul; and, I have a backend service which returns a HATEOS response.

    The links from the API Gateway was always returning the links from backend service. I was told that the X-forward-Host was not being sent by the requesting http client and had no idea till I saw this post.

    If you have a simpler way of sending the header other than an interceptor, please let me know.

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *