Spring Web - Rest Client

. . 4 min read


Spring Framework 6.1 introduces RestClient a new Fluent API to make synchronous HTTP requests

What You Will build

You will build a Spring Boot web application that consumes and external api with RestClient.

What You Need

  • A favorite text editor or IDE
  • JDK 1.8 or later
  • Gradle 4+ or Maven 3.2+

Setup Project With Spring Initializr

  • Navigate to Spring initializr.
  • Define the project name example: spring-web-rest-client
  • Choose Project Maven and the language Java.
  • Choose Your Java version ex: 17
  • Click add dependencies and select:
    • Spring Web
  • Make sure the Spring Version at least is : 3.2
  • Click Generate.

Unzip the Downloaded Zip and open the Project using your favorite text editor or IDE


In this tutorial, we’re going to demonstrate a range of operations where and How the RestClient can be used.

Instantiate the RestClient

In order to do that you can simply use the static method create()

private final RestClient restClient = RestClient.create();

Or you can use the builder for more configuration, eg: set the base url , set default headers...

private final RestClient restClient = RestClient.builder()

Retrieve Resource(s)

  • Using the toEntity to return ResponseEntity with the body of a given type
  • Once you get the ResponseEntity object you can check the response statusCode and the headers
// retrieve a list of products
ResponseEntity<List<Product>> response = restClient.get()
    .toEntity(new ParameterizedTypeReference<>() {});
// retrieve a product by id
ResponseEntity<Product> response = restClient.get()

Post Resource

To perform a POST request you just need to use the post() and specify the contentType and the body

ResponseEntity<Product> response = restClient.post()

Delete Resource

To perform a DELETE request you just you need to use the delete()

 ResponseEntity<Product> response = restClient.delete()
            .uri("/products/{productId}", productId)

Error handling

RestClient throws RestClientResponseException when receiving a 4xx or 5xx status code. You can catch the exception and throw a customized exception instead:

ResponseEntity<Product> response = restClient.get()
            .onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
                throw new ResponseStatusException(response.getStatusCode(), response.getHeaders())

For more flexibility of how you want to handle the response, the exchange would be the option to go with, also you need to provide an explicit mapping for your desire Type, eg : ObjectMapper

ObjectMapper mapper = new ObjectMapper();
Product product = restClient.get()
    .uri("/products/{productId}", productId)
    .exchange((req,res) -> {
        if (res.getStatusCode().is2xxSuccessful()) {
            return  mapper.readValue(res.getBody(), Product.class);
        if (res.getStatusCode().is4xxClientError()) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "the product does not exist");
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "something went wrong");


Congratulations πŸŽ‰ ! You've covered the main features of the new api RestClient, You can notice that the new API is more straightforward (fluent) to handle Http Requests compared to the old RestTemplate


The tutorial can be found here on GitHub πŸ‘‹


Check new tutorials on nonestack πŸ‘‹


Recent articles

Spring Authorization Server - OAuth JWT

spring authorize server

This guide walks you through the process of building a Spring Boot Authorization Server JWT that uses Spring Security Security and Spring Security OAuth Resource. Applying the new way to configure Spring Authorization Server JWT without the Custom Filter

Continue reading
9 min read

Spring Data - JPA DataTable

spring data

This guide walks you through the process of building a Spring boot application that uses JPA DataTable.

Continue reading
10 min read

Spring Security - Basic Authentication

spring security

This guide walks you through the process of building a Spring boot application that uses Spring Security and Spring Data JPA. Applying the new way to configure Spring Security without the WebSecurityConfigurerAdapter

Continue reading
6 min read

Spring Boot - Get Started

spring boot

Spring Boot is a popular framework that simplifies and accelerate the process of developing a java based application

Continue reading
5 min read