Sending Headers in Feign Client

In the modern world of microservices, communication between different services is crucial. Feign, a web service client that simplifies the communication between microservices . One of its powerful features is the ability to send custom headers with requests, which can be achieved using the @RequestHeader annotation or a Request Interceptor.
Sending Headers with
@RequestHeaderAnnotation
The @RequestHeader annotation is straightforward and convenient for adding headers to specific methods in your Feign client. Here's how you can use it:
First, create an interface and annotate it with @FeignClient. This tells Spring to create a bean for this interface and handle the communication.
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
@FeignClient(name = "example-client", url = "http://example.com")
public interface ExampleClient {
@GetMapping("/api/v1/resource")
String getResourceV1(@RequestHeader("Custom-Header") String headerValue);
@GetMapping("/api/v2/resource")
String getResourceV2(@RequestHeader Map<String,String> headerValues);
}By the way you can send also a multiple headers using a Map .
You can now autowire and use this Feign client in your Spring service or controller .
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ExampleService {
@Autowired
private ExampleClient exampleClient;
public String fetchResourceV1() {
return exampleClient.getResource("HeaderValue");
}
public String fetchResourceV2() {
Map<String,String> headerValues = new HashMap<>();
headerValues.put("Custom-Header-1","HeaderValue1");
headerValues.put("Custom-Header-2","HeaderValue2");
return exampleClient.getResource(headerValues);
}
}Sending Headers with a Request Interceptor
While the @RequestHeader annotation is great for specific cases, sometimes you need to add headers to all requests made by a Feign client. For this, you can use a Request Interceptor.
Create a class that implements RequestInterceptor and override the apply method to add headers .
import feign.RequestInterceptor;
import feign.RequestTemplate;
@Configuration
public class MyRequestInterceptor implements RequestInterceptor{
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("Custom-Header", "HeaderValue");
}
}Update your Feign client to not use the RequestHeader annotation .
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
@FeignClient(name = "example-client", url = "http://example.com")
public interface ExampleClient {
@GetMapping("/api/v1/resource")
String getResourceV1();
}You can now autowire and use this Feign client in your Spring service or controller, and the custom header will be included in every request.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ExampleService {
@Autowired
private ExampleClient exampleClient;
public String fetchResource() {
return exampleClient.getResource();
}
}Choosing the Right Approach
- Use
@RequestHeaderAnnotation: When you need to add headers for specific methods only. This approach is simple and localized to the method where the header is needed. - Use Request Interceptor: When you need to add headers to all requests made by a Feign client. This approach is more centralized and ensures consistency across all requests.
Conclusion
Feign clients offer a powerful and flexible way to communicate between microservices. By understanding and utilizing the @RequestHeader annotation and Request Interceptors, you can ensure that your requests are properly authenticated and customized to meet your application's needs.
Both methods have their use cases, and knowing when to use each will help you build more robust and maintainable microservices. Happy coding!






