codingsplash
codingsplash

Reputation: 5075

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

Upvotes: 61

Views: 89775

Answers (7)

Pankaj Singh
Pankaj Singh

Reputation: 148

I know its old question even after this, As per me there are following major benefits of using feign clients over rest templates.

  1. Developer has not to write any code except feign interface, so there is no scope of error or unit test.

  2. No hardcoding of URL, in conjunction of Eureka naming server only service name id is enough to call the service.

  3. Provide load balancing feature automatically with Eureka.

Upvotes: 3

slim
slim

Reputation: 31

Advantages of using Feign over RestTemplate:

Declarative approach: Feign provides a more declarative approach to define and use REST API clients, which can make the code more readable and easier to maintain.

Integrated with Eureka: Feign is integrated with Netflix Eureka for service discovery, making it easier to build and consume APIs in a microservices architecture.

Better error handling: Feign provides better error handling, including support for custom error handling and retries.

Support for multiple encodings: Feign supports multiple encoding types, including JSON, XML, and form data, while RestTemplate only supports JSON and XML.

Disadvantages of using Feign over RestTemplate:

Limited flexibility: Feign provides a more opinionated approach to defining and using REST API clients, which may limit flexibility in certain situations.

Limited control over HTTP request and response: Feign abstracts away some of the low-level details of the HTTP request and response, which can make it harder to control and customize these details if needed.

Lack of official support: Feign is not an officially supported library from Spring, which may be a consideration for some developers or organizations.

Upvotes: 3

Gonen I
Gonen I

Reputation: 6127

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.

Upvotes: 38

FBA Gimhana
FBA Gimhana

Reputation: 329

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 ...

Upvotes: 7

Sankalpa Wijewickrama
Sankalpa Wijewickrama

Reputation: 1275

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.

Upvotes: 8

Alien
Alien

Reputation: 15908

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.

Upvotes: 25

Andy
Andy

Reputation: 6349

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.

Upvotes: 15

Related Questions