Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages and disadvantages of using feign over RestTemplate

I get that Feign is declarative and hence it abstracts out a lot of things for the developer. But, when should one choose one over the other? Though feign is declarative, it has serious problems with oAuth. What are some of the considerations in using RestTemplate over Feign

like image 557
codingsplash Avatar asked Oct 23 '17 07:10

codingsplash


People also ask

What is the advantage of Feign over RestTemplate?

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

Which is better REST template or feign client?

When we use the RestTemplate to call the RESTful service, it creates duplication of code that talks to RESTful services. When we define Feign, we need only to define a proxy and define a single method into it. Feign helps us to simplify client code to talk to the RESTful web services.

Why do we use feign client?

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations.

Does feign client internally use RestTemplate?

Feign client is really convenient tool to use. But I recently came to know that Rest-Template is going to be deprecated and will be replaced by WebClient, and Feign Client internally uses Rest-Template.


5 Answers

Feign allows you to abstract the mechanics of calling a REST service. Once you configure and annotate the Feign interface, 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 your business logic code.

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

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

like image 139
Gonen I Avatar answered Oct 04 '22 14:10

Gonen I


There are certain advantages.

1.URLs are not hardcoded.

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

3.we can use Eureka Client ID instead of the URL.

4.Feign handled the actual code.

5.Feign integrates with Ribbon and Eureka Automatically.

6.Feign provides a very easy way to call RESTful services.

like image 34
Alien Avatar answered Oct 02 '22 14:10

Alien


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

like image 31
Andy Avatar answered Sep 30 '22 14:09

Andy


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

  1. The developer need not worry about the implementation. Just to create abstract Feign interface and few annotations - declarative principle. (If you want customized configuration, then it will hold some code)

  2. With Spring Cloud Eureka, Ribbon client-side load-balancer will be equipped with Feign client.

  3. No need to worry about the unit test, because there is no implementation from you to test. (Arguable)

  4. Supports Feign annotations and JAX-RS annotations.

  5. Highly compatible and easily configurable with Spring Cloud (Specially with Eureka server registry)

  6. Allows Feign client configuration via @Configuration class or application properties.

  7. Allows us to add interceptors. (Add interceptors via @Configuration or application properties. Alternatively can use Spring Cloud provided interceptors as well. Example - BasicAuthRequestInterceptor)

  8. Hystrix support for fall-back mechanism.

  9. Logging

  10. Error handling

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

like image 39
Sankalpa Wijewickrama Avatar answered Oct 01 '22 14:10

Sankalpa Wijewickrama


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

Feign is a Spring Cloud Netflix library for providing a higher level of abstraction over REST-based service calls. Spring Cloud Feign works on a declarative principle. When using Feign, we write declarative REST service interfaces at the client, and use those interfaces to program the client. The developer need not worry about the implementation ...

like image 45
FBA Gimhana Avatar answered Oct 03 '22 14:10

FBA Gimhana