Spring RestTemplate vs RestClient


When building applications that interact with external APIs, making HTTP requests is a common requirement. In the Spring ecosystem, two major options are available:

  • RestTemplate – the classic synchronous HTTP client (legacy but still widely used).
  • RestClient – the modern, fluent, and more intuitive replacement introduced in Spring Framework 6.

In this blog, we’ll cover both, compare their features, and see how to use them with practical code snippets.

1. RestTemplate – The Classic HTTP Client

RestTemplate has been the go-to HTTP client in Spring for years. It’s easy to use, synchronous, and supports various HTTP methods. However, it’s considered deprecated in favour of WebClient (for reactive) and RestClient (for synchronous).

Example: GET Request using RestTemplate

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestTemplateExample {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://jsonplaceholder.typicode.com/posts/1";

        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

        System.out.println("Status Code: " + response.getStatusCode());
        System.out.println("Response Body: " + response.getBody());
    }
}

Example: POST Request using RestTemplate

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class RestTemplatePostExample {

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://jsonplaceholder.typicode.com/posts";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("title", "Spring Blog");
        requestBody.put("body", "Exploring RestTemplate in Spring");
        requestBody.put("userId", 1);

        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);

        String response = restTemplate.postForObject(url, request, String.class);

        System.out.println("Response: " + response);
    }
}

Pros of RestTemplate: Simple, easy to use, widely available in legacy projects.
⚠️ Cons: Verbose, less fluent, considered deprecated.

2. RestClient – The Modern Successor

Spring RestClient was introduced in Spring Framework 6 / Spring Boot 3. It provides a fluent API, better readability, and replaces RestTemplate for synchronous HTTP calls.
It is not reactive like WebClient but much simpler than RestTemplate.
Example: GET Request using RestClient

import org.springframework.web.client.RestClient;

public class RestClientExample {

    public static void main(String[] args) {
        RestClient restClient = RestClient.create();

        String url = "https://jsonplaceholder.typicode.com/posts/1";

        String response = restClient.get()
                .uri(url)
                .retrieve()
                .body(String.class);

        System.out.println("Response: " + response);
    }
}

Example: POST Request using RestClient

import org.springframework.http.MediaType;
import org.springframework.web.client.RestClient;

import java.util.HashMap;
import java.util.Map;

public class RestClientPostExample {

    public static void main(String[] args) {
        RestClient restClient = RestClient.create();

        String url = "https://jsonplaceholder.typicode.com/posts";

        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("title", "Spring 6 Blog");
        requestBody.put("body", "Exploring RestClient in Spring");
        requestBody.put("userId", 1);

        String response = restClient.post()
                .uri(url)
                .contentType(MediaType.APPLICATION_JSON)
                .body(requestBody)
                .retrieve()
                .body(String.class);

        System.out.println("Response: " + response);
    }
}

Pros of RestClient:

  • Fluent API (chainable methods).
  • Cleaner and less boilerplate code.
  • Built for Spring 6 and Boot 3+.

⚠️ Cons:

  • Only available in newer Spring versions.
  • Less common in older enterprise projects.
Spring RestTemplate vs RestClient

3. Key Differences Between RestTemplate and RestClient

FeatureRestTemplateRestClient (Spring 6)
Introduced inSpring 3 (legacy)Spring 6 / Boot 3
API StyleVerbose, less fluentFluent, modern, chainable
Reactive Support❌ (Blocking only)❌ (Blocking only)
Future SupportDeprecated (maintenance mode only)Actively developed
Best Use CaseLegacy Spring appsNew Spring Boot apps (3.x onwards)

Surmmary

RestTemplate has served the Spring ecosystem for more than a decade, but with the introduction of RestClient, developers now have a cleaner and more modern API for making synchronous HTTP calls.

References

  1. REST Clients
  2. RestTemplate
  3. Spring Boot – Calling REST Services with RestTemplate
  4. Spring Boot– Consuming a REST Services with WebClient

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.