blog.stackademic.com-Integrations in Spring Boot with FeignClient
blog.stackademic.com-Integrations in Spring Boot with FeignClient
blog.stackademic.com/integrations-in-spring-boot-with-feignclient-e2efe59d3d17
Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to
democratize free coding education for the world.
Imagine a client wants from you to develop an application about quotes. Client wants to
generate random quotes in application and save them in a list to check it again from his/her
profile. Client also wants to generate quotes by author name, quote category etc.
First of all, you need to develop a backend API in order to establish communication between
client and server, perform operations and create business logic of the application.
You also found an external API that generates quotes, you want to send request to that
external API to receive a random quote and save it to database.
In order to use that external API, you need to integrate it to your API and use it from there,
you can perform it by using @FeignClient of Java Spring Boot.
1/6
What is Feign Client?
“Feign” refers to a declarative web service client that simplifies the process of making HTTP
requests to other RESTful services. Feign is part of the Spring Cloud ecosystem, which
provides tools and libraries for building cloud-native applications.
With Feign, you define the structure of your HTTP requests using a simple Java
interface, making the code clean and easy to understand. This approach reduces
boilerplate code and makes the communication with remote services more concise.
Feign is a core component in the Spring Cloud ecosystem, and it integrates well with
other Spring Cloud tools and libraries like Eureka for service discovery, Hystrix for
circuit breaking, and Sleuth for distributed tracing. This makes it a powerful tool for
building resilient and scalable microservices.
Feign clients can be easily mocked and tested in isolation, allowing for thorough unit
testing of your client code.
First of all, you should create your Spring Boot application from start.spring.io with
dependencies and other versions you select for your application.
After you create your application, you should add dependencies to your project.
2021.0.3
org.springframework.cloudspring-cloud-starter-openfeign4.0.2
After you add this dependencies, don’t forget to run this command in order to install it
correctly:
2/6
ext { springCloudVersion = }
dependencies { implementation }
gradle build
After you installed dependencies, you should enable feign clients in your
@SpringBootApplication to use them.
{ SpringApplication.run(YourApplication.class, args); }
will enable usage of Feign in other components of your Spring Boot application.
Before integrating external API, please check https://api-ninjas.com/api/quotes for how does
API work, what request should you send and what response will you receive after a
succesful request. Don’t forget to sign up to retrieve your API Key for sending requests. In
further sections, you will see in details how to send request and use API Key.
Let’s create an interface that will use to send requests to our external API.
List<QuoteResponse> ;}
value: The name of the target service (usually it’s the same name with interface but
lowercased).
url: A base url for external API so that you don’t have to repeat in request
3/6
@RequestHeader: Binds request headers to method parameters. In our example “X-Api-
Key” is a header value that we should give to send the request.
So this interface is used to send requests to external API and get responses based on
success of request.
As we said earlier, we should read documentation and check for external API as it will return
with a response and we should create a response with the same values of that response.
So we need to create a response in our Spring Boot application that will return in our API as
a response from external API.
This class will be a response type that will return us as a response when we send the
request. As you can see, variable names are the same with the key names of JSON
response coming to us.
If you want to use a different name in your response, you can use to specify which key of
JSON value specifies variable at, here is an example:
As you can see, we changed the name of quote variable to but we added a so when we get
response, it will map automatically the quote key value with variable.
As we created our response, let’s create a service that will handle business logic of our
QuoteClient.
4/6
{
List<QuoteResponse> { quoteClient.generateQuote(category,
token); }}
QuoteService will call generateQuote function from QuoteClient and return as list of
QuoteResponse.
annotation is used to extract a value from application.properties, don’t forget to hide token in
application.properties so when you publish code in GitHub or deploy it, other users won’t try
to use it.
Some responses are returned as lists as they may contain more than one JSON objects so
some API’s may return their responses as list, in our API it returns it as list so we should
return as to not have deserialization issues with response.
Lastly, we should create a controller that will handle requests of our API.
QuoteService quoteService;
ResponseEntity<List<QuoteResponse>> {
ResponseEntity.ok(quoteService.generateQuote(category)); }}
QuoteController has a request with Query Parameter named category that will call function
from QuoteService class and it will call function of QuoteClient to send requests to external
API and return us a response.
5/6
Sent request from Postman and returned response.
As you can see, when we send request to our endpoint, it returns a response from external
API with the same response type.
You can use for your integrations if you will integrate an API that has a base URL and just
create GET, POST requests from your client with the parameters, body or headers as
external API requires.
Stackademic
Thank you for reading until the end. Before you go:
6/6