06: Spring Cloud declarative REST client Feign

Q. What use Feign client?
A. Feign is a declarative web service client, which makes creation of web service clients a breeze.

1) Unlike a RestTemplate, Feign client acts as a load balancer. Spring Cloud integrates Netflix/ribbon and Netflix/eureka to provide a load balanced http client when using Feign.

2) Due to is declarative nature, does not require any unit tests to be written. Create an interface and annotate it with Feign and JAX-RS annotations. Feign supports pluggable annotations, encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters as used in Spring Web.

Step 1: Go to https://start.spring.io/ to create a skeleton project for a web service REST client with Feign.

Spring Feign Client

Lombok is a java library that automatically plugs into your editor and build tools, and speed up your development by never have to write another getter or equals method again. Just with one annotation your class has a fully featured builder, automated logging variables, and much more.

Spring Boot 1.3 ships with spring-boot-devtoolsto try and improve the development-time experience when working on Spring Boot applications. DevTools automatically restart your running application with the new changes applied. When developing locally, this can be valuable as you don’t need to redeploy your application manually. The usage of the Spring Boot DevTools is intended only for development, not for production. If your application detects you’re running in production, DevTools are automatically disabled.

Click on “generate” to download the “my-feign-client.zip” file.

Step 2: Unzip “my-feign-client.zip” inside the “projects folder” and open this folder with your IDE of your choice. I am opening the with “Visual Studio Code” IDE.

Step 3: The pom.xml file.

Step 4: Let’s consume an existing online producer service via JSONPlaceholder.

JSONPlaceholder

Let’s test: https://jsonplaceholder.typicode.com/posts

Step 5: Create a POJO named “Post.java” under “src/main/java” folder and package “com.example.myfeignclient.dto“.

Post.java

Step 6: Create an interface “PostClient.java” in folder “src/main/java” under the package “com.example.myfeignclient.client“.

PostClient.java

Step 6: Modify “MyFeignClientApplication.java” in package “com.example.myfeignclient” and folder “src/main/java“.

Step 7: Build & Run.

Build

Run

Step 8: Open a browser and type:

URL: http://localhost:8080/findAllPosts

findAllPosts

You can also use the browser extension Postman via Chrome or Firefox to make RESTful calls.

Project Structure

Spring Feign Client Tutorial


print


Why wait 5-10 when you can fast-track in 1-2 ?

open all | close all
Top