Search code examples
spring-bootspring-webclientopenfeignreactive-feign-client

SpringBoot FeignClient vs WebClient


I want to consume a couple of rest services. Before, I used RestTemplate, but now I want to know The main differences between Spring Boot FeignClient and WebClient.

When should they be used?


Solution

  • To be able to answer “when” one needs to understand the capabilities of each.

    Spring WebClient is a non-blocking reactive client to make HTTP requests. Hence if you intend to use Spring Reactive Stream API to stream data asynchronously then this is the way to go. Think event-driven architecture. WebClient is part of the Spring WebFlux library.

    [Feign]3 is a declarative REST library that uses annotations based architecture with thread-per-request model. This means that the thread will block until the feign client receives the response. The problem with the blocking code is it must wait until the consuming thread completes, hence think memory and CPU cycles.

    So use Spring WebClient when needing non-blocking HTTP requests otherwise Feign due to simple usage model.

    (Note: There is no reason as to why one cannot use WebClient for blocking operations but Feign is more mature and it’s annotation based model makes it easier)