WebClient vs RestTemplete

Bosky
2 min readJan 28, 2023

In this article we will compare Spring WebClient and RestTemplete. Spring WebClient and RestTemplate are both libraries in the Spring Framework that are used to make HTTP requests.

Spring WebClient is a non-blocking, reactive client for HTTP requests. It is designed to handle high concurrency and is well-suited for use in a reactive programming model.

RestTemplate is a blocking, synchronous client for HTTP requests. It is a simpler and more traditional approach for making HTTP requests and is easier to use for simple, one-off requests.

RestTemplete

RestTemplate is a blocking client, which means that it uses a separate thread for each incoming request and that thread remains blocked until the response is received.

In scenarios where the application receives a large number of requests, and each request takes one second to process, the requests will accumulate and each request will be associated with a thread. These threads consume CPU cycles and memory, which can eventually lead to a decline in the performance of the application.

Spring WebClient is a non-blocking, reactive client for HTTP requests in the Spring Framework. It is designed to handle high concurrency and is well-suited for use in a reactive programming model.

WebClient

WebClient uses a non-blocking, event-driven architecture which means that it does not block any thread while waiting for a response. Instead, it utilizes a reactive programming model where the response is returned as a stream of events. This allows WebClient to handle a large number of requests with a small number of threads, which can result in better performance and scalability.

Example

We will be utilizing a basic greeting service, and will be making calls to this service using both RestTemplate and WebClient libraries in Spring Framework, to compare their usage and performance.

@GetMapping("/greet")
public String greet() throws InterruptedException {
Thread.sleep(1000);
return "Hello "+LocalDateTime.now();
}

Using RestTemplete to call greeting service


@GetMapping("restclient")
public ResponseEntity<String> restClient() {
return ResponseEntity.ok().body(client.callWithResttemplate());
}

public String callWithResttemplate() {
String val=restTemplate.getForEntity("http://localhost:8080/greet", String.class).getBody();
log.info(val);
return val;
}

I have used Jmeter to calculate the performance

JMeter RestTemplete

Using WebClient to call greeting service

@GetMapping("fluxclient")
public Mono<String> fluxClient() {
return client.callWebClient();
}

public Mono<String> callWebClient() {
log.info("in call");
Mono<String> val = webClient.get().uri("/greet").retrieve().bodyToMono(String.class);
return val;
}
JMeter WebClient

Conclusion

The graph clearly shows that when using WebClient, the throughput is significantly higher and the response time is shorter compared to when using RestTemplate.

source code available here https://github.com/boskyj/mywork/tree/master

--

--