Skip to main content
Please wait...
The Change from RestTemplate to Spring Webflux
13 Apr, 2025

The Change from RestTemplate to Spring Webflux

Spring Framework's RestTemplate Class

RestTemplate is a class in the Spring Framework that simplifies making HTTP requests. It provides methods for interacting with RESTful services, handling HTTP requests and responses, and converting them to Java objects.

 

Common Scenarios for Using RestTemplate 

Spring developers commonly use RestTemplate for: 

  • Consuming RESTful APIs: Making GET, POST, PUT, DELETE requests to external APIs.

  • Microservices Communication: Facilitating communication between microservices in a distributed system. 

  • Handling HTTP Responses: Automatically converting HTTP responses to Java objects.

Synchronous vs. Asynchronous Clients 

  • Synchronous Client: Blocks the calling thread until the operation completes. RestTemplate is a synchronous client. 

  • Asynchronous Client: Does not block the calling thread. It uses callbacks or futures to handle the response. WebClient can be used as an asynchronous client.

Implementing RestTemplate with Apache HttpComponents 

To implement a RestTemplate instance with the Apache HttpComponents library in a Spring Boot REST API, you can configure it as follows: 

import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
import org.springframework.web.client.RestTemplate; 
 
@Configuration 
public class RestTemplateConfig { 
 
    @Bean 
    public RestTemplate restTemplate() { 
        CloseableHttpClient httpClient = HttpClients.createDefault(); 
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); 
        return new RestTemplate(factory); 
    } 
} 
  

WebClient as a Replacement for RestTemplate 

WebClient is part of the Spring WebFlux module and is designed to be a non-blocking, reactive alternative to RestTemplate. It supports both synchronous and asynchronous operations.

Spring WebFlux's Support for Reactive Streams 

Spring WebFlux is built on the reactive programming model and supports reactive streams. It leverages Project Reactor to provide a reactive and non-blocking approach to web applications, enabling efficient handling of large numbers of concurrent connections.

WebClient Interface 

The WebClient interface is a non-blocking, reactive client used to perform HTTP requests. It leverages the reactive programming model to handle asynchronous operations efficiently.

Comparing RestTemplate and WebClient 

RestTemplate: 

  • Synchronous 

  • Simpler API 

  • Suitable for traditional, blocking applications

 

WebClient: 

  • Supports both synchronous and asynchronous operations

  • Reactive and non-blocking 

  • Suitable for modern, reactive applications

  • More flexible and powerful

HTTP Client Libraries Supported by WebClient 

WebClient supports various HTTP client libraries, including: 

  • Netty

  • Apache HttpClient

  • Jetty 

Change from Spring Web MVC to Spring WebFlux 

Spring Web MVC is based on the traditional, blocking model of handling web requests, suitable for synchronous operations. Spring WebFlux, on the other hand, is designed for non-blocking, reactive applications, providing better scalability and performance for handling large numbers of concurrent connections. 

Implementing Netty HttpClient with WebClient in Spring Boot 

To implement the Netty HttpClient with WebClient in a Spring Boot application, you can configure it as follows: 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.reactive.function.client.WebClient; 
import reactor.netty.http.client.HttpClient; 
import org.springframework.http.client.reactive.ReactorClientHttpConnector; 
 
@Configuration 
public class WebClientConfig { 
 
    @Bean 
    public WebClient webClient() { 
        HttpClient httpClient = HttpClient.create(); 
        return WebClient.builder() 
                .clientConnector(new ReactorClientHttpConnector(httpClient)) 
                .build(); 
    } 
} 
  

pom.xml Configuration 

 

To include the necessary dependencies in your pom.xml, you can add the following:

<dependencies> 
    <!-- Spring Boot Starter Web --> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
 
    <!-- Spring Boot Starter WebFlux --> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-webflux</artifactId> 
    </dependency> 
 
    <!-- Apache HttpComponents --> 
    <dependency> 
        <groupId>org.apache.httpcomponents</groupId> 
        <artifactId>httpclient</artifactId> 
    </dependency> 
 
    <!-- Reactor Netty --> 
   >io.projectreactor.netty</groupId> 
        <artifactId>reactor-netty</artifactId> 
    </dependency> 
</dependencies> 

References 

  • Weerasinghe, Sidath, and Indika Perera. "Optimized Strategy in Cloud-Native Environment for Inter-Service Communication in Microservices." International Journal of Online & Biomedical Engineering 20.1 (2024). 

  • Späth, Peter, et al. "Reactive Spring." Pro Spring 6 with Kotlin: An In-depth Guide to Using Kotlin APIs in Spring Framework 6. Berkeley, CA: Apress, 2023. 855-913.

  • Katamreddy, Siva Prasad Reddy, and Sai Subramanyam Upadhyayula. "Reactive Programming Using Spring WebFlux." Beginning Spring Boot 3: Build Dynamic Cloud-Native Java Applications and Microservices. Berkeley, CA: Apress, 2022. 185-213.

  • Deinum, Marten, et al. "Building reactive applications with spring webflux." Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 (2021): 369-420.