What are the advantages and disadvantages of using feign over RestTemplate

Categories: Feign RestTemplate Spring Cloud Netflix

 When using RestTemplate,the URL parameter is constructed programmatically, and data is sent across tothe other service. In more complex scenarios, we will have to get to the details of the HTTP APIs provided byRestTemplate or even to APIs at a much lower level.

Feign isa Spring Cloud Netflix library forproviding a higher level of abstraction over REST-based service calls. Spring Cloud Feign works on a declarative principle. When usingFeign, we write declarative REST service interfaces at the client, and usethose interfaces to program the client. Thedeveloper need not worry about the implementation.

 

Using Feign-clients overrest-templates has number of advantages. I will list down those below.

 

Feign is a good choice, If you arefascinated with JPA and the way how it resolves your queries, then Feign is thetool for you. Feign will handle your server requests perfectly fine

One of the advantages of using Feign over RestTemplate is that, we do not need to write anyimplementation to call the other services. So there is no need to write any unit test asthere is no code to test in the first place. However, it is advised that wewrite Integration tests.

 

There are certain advantages.

1.URLs are not hardcoded.

2.you don't have to write unit testcases for feign as there is no code to test however you have to writeintegration tests.

3.we can use Eureka Client ID insteadof the URL.

4.Feign handled the actual code.

5.Feign integrates with Ribbon andEureka Automatically.

6.Feign provides a very easy way tocall RESTful services.

Feign allows you to abstract themechanics of calling a REST service. Once you configure and annotate the Feigninterface, you can call a REST service by making a simple Java function call.The actual implementation of making a REST call is handled at runtime by Feign.This means that the implementation can be configured without changing yourbusiness logic code.

By just changing the Feignconfiguration in Java or using properties you can add encoding/decoding,logging, and change the REST call implementation library. All this is donethrough configuration only, while the business logic that calls the serviceremains unchanged.

Since Feign uses standardJava interfaces, it's also easy to mock them during unit tests.

R4Rin Team
The content on R4Rin.com website is created by expert teams.