Simplify REST client and reduce boilerplate code with Feign
Intro
RestTemplate remains the most popular way to consume RESTful Web Services in Spring Framework. Although we are all aware that the library is not perfectly designed and numerous arguments in method signature contradict commonly known clean-code principles, we’ve just got used to it.
Fortunately, there is an alternative called Feign! With Feign we don’t litter our business logic with details of the HTTP client. We declare what REST client should do, instead of how it should be done.
Advantages of using Feign:
- simple configuration by annotating interface
- no need to write unit tests since there is no implementation (although integration tests are recommended!)
- highly compatible and easily configurable
- built-in Hystrix fall back mechanism
- adapted to work in microservice environment (easy integration with Eureka and Ribbon providing Client-Side Load Balancing)
Creating Feign Client
Now it is time to dive into the world of Feign and create the first client. The dependency you need is spring-cloud-starter-openfeign and you are ready to go! Let’s start with enabling feign clients in our application.
Then we declare the actual client.
Simple, right? We’ve just created a working REST client! Currently it has only one method that returns a list of users.
Let’s go one step further and implement PUT method to update user. Imagine that the target API is secured with Basic Authorization and we’d like our client to log full information. In addition, we would like to have a custom error handler.
Firstly, we start with updating client class. New method updateUser was declared. It takes two arguments: the first one is user identifier, the second one is simple POJO class representing updated user object. In annotation we also defined, that we want to send data in JSON format. Additionally, we defined custom configuration class on the interface level.
Second step is to implement the configuration and create all required beans. We will use an Interceptor to enable adding Authorization Header to all requests. Next bean overrides the default logging level and the last one creates a custom error handler.
And what else is missing is the implementation of the custom error handler.
Feign Configuration
To configure Feign Client, Spring looks up beans named below, the white ones are provided by default, grey ones must be provided additionally, when needed.
Feign Client can be also configured using configuration properties like presented below.
application.yaml
Testing
Feign client can easily be tested using WireMockServer provided by spring-cloud-starter-contract-stub-runner dependency. We must only define a stub expected behavior and et voilà! We are ready to call our Feign client and verify request and response.
Console log output
Conclusions
Feign is a powerful library, created to simplify implementing API clients. Additionally, it prevents from generating boilerplate code and lets developer focus on the business logic rather than paying too much attention to HTTP client.
I hope you use Feign in your next project! You can find full demo source code here