Spring Web - Rest Client

. . 4 min read

display

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

Overview

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()
    .baseUrl("http://localhost:8081/api")
    .build();

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()
    .uri("/products")
    .retrieve()
    .toEntity(new ParameterizedTypeReference<>() {});
// retrieve a product by id
ResponseEntity<Product> response = restClient.get()
    .uri("/products")
    .retrieve()
    .toEntity(Product.class);

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()
    .uri("/products")
    .contentType(APPLICATION_JSON)
    .body(product)
    .retrieve()
    .toEntity(Product.class);

Delete Resource

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

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

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()
            .uri("/products")
            .retrieve()
            .onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
                throw new ResponseStatusException(response.getStatusCode(), response.getHeaders())
             })
            .toEntity(Product.class);

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");
    });

Summary

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

Github

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

Blog

Check new tutorials on nonestack πŸ‘‹

35
1072

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
.
1115 views

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
.
853 views

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
.
923 views

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
.
874 views