Build a REST Client to Test Spring Boot CRUD RESTful Web Services [2025 Edition]
In our previous tutorial, we built a Spring Boot CRUD REST API for User Management using Spring Data JPA and MySQL. Typically, we use Postman to test REST APIs, but in this tutorial, we will build our own REST Client using Spring WebClient.
The prerequisite for this tutorial is you need to first Build RESTful Web Services Using Spring Boot, Spring Data JPA, and MySQL
π Why Build a REST Client?
β
Automates API testing inside the application.
β
Eliminates the need for external tools like Postman.
β
Useful for microservices communication.
β
Provides a programmatic way to consume REST APIs.
For non-members, read this article for free on my blog: Build a REST Client to Test Spring Boot CRUD RESTful Web Services.
π Step 1: Add WebClient Dependency to pom.xml
π Ensure the spring-boot-starter-webflux
dependency is added
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
WebClient is a non-blocking, reactive alternative to RestTemplate that supports asynchronous and synchronous calls.
π Step 2: Configure WebClient
To make API calls, we must define a base URL for our User Management REST API.
π Create WebClientConfig.java
inside net.javaguides.usermanagement.client
package net.javaguides.usermanagement.client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) {
return builder.baseUrl("http://localhost:8080/api/users").build();
}
}
π Explanation
βοΈ @Configuration
β Marks this class as a Spring configuration file.
βοΈ Defines a WebClient
Bean that connects to http://localhost:8080/api/users
.
βοΈ Centralized configuration allows easy URL modification.
π Step 3: Implement the REST Client
Now, letβs create a REST Client that will:
β
Create a new user
β
Fetch a user by ID
β
Fetch all users
β
Update a user
β
Delete a user
π Create RESTClient.java
inside net.javaguides.usermanagement.client
package net.javaguides.usermanagement.client;
import net.javaguides.usermanagement.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.time.LocalDate;
import java.util.List;
@Component
public class RESTClient {
@Autowired
private WebClient webClient;
// Create a new user
public UserDto createUser() {
UserDto newUser = new UserDto(
null,
"Ravi",
"Kumar",
"ravi.kumar@example.com",
LocalDate.of(1995, 8, 15)
);
return webClient.post()
.body(Mono.just(newUser), UserDto.class)
.retrieve()
.bodyToMono(UserDto.class)
.block(); // Blocking for simplicity (not recommended in production)
}
// Get user by ID
public UserDto getUserById(Long id) {
return webClient.get()
.uri("/{id}", id)
.retrieve()
.bodyToMono(UserDto.class)
.block();
}
// Get all users
public List<UserDto> getAllUsers() {
return webClient.get()
.retrieve()
.bodyToFlux(UserDto.class)
.collectList()
.block();
}
// Update a user by ID
public UserDto updateUser(Long id) {
UserDto updatedUser = new UserDto(
id,
"Ravi",
"Kumar",
"ravi.updated@example.com",
LocalDate.of(1995, 8, 15)
);
return webClient.put()
.uri("/{id}", id)
.body(Mono.just(updatedUser), UserDto.class)
.retrieve()
.bodyToMono(UserDto.class)
.block();
}
// Delete a user by ID
public void deleteUser(Long id) {
webClient.delete()
.uri("/{id}", id)
.retrieve()
.toBodilessEntity()
.block();
}
}
π Explanation of RESTClient.java
βοΈ @Component
β Registers this class as a Spring Bean.
βοΈ Uses WebClient
to interact with the REST API.
βοΈ block()
is used for simplicity but should be replaced with reactive programming in production.
π Step 4: Create a CommandLineRunner to Test the Client
We need a way to execute our REST client when the application starts.
π Modify UserManagementApplication.java
inside net.javaguides.usermanagement
package net.javaguides.usermanagement;
import net.javaguides.usermanagement.client.RESTClient;
import net.javaguides.usermanagement.dto.UserDto;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UserManagementApplication implements CommandLineRunner {
private final RESTClient restClient;
public UserManagementApplication(RESTClient restClient) {
this.restClient = restClient;
}
public static void main(String[] args) {
SpringApplication.run(UserManagementApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("------ REST Client Test Started ------");
// Create a user
UserDto createdUser = restClient.createUser();
System.out.println("Created User: " + createdUser);
// Get user by ID
UserDto retrievedUser = restClient.getUserById(createdUser.id());
System.out.println("Retrieved User: " + retrievedUser);
// Get all users
System.out.println("All Users: " + restClient.getAllUsers());
// Update the user
UserDto updatedUser = restClient.updateUser(createdUser.id());
System.out.println("Updated User: " + updatedUser);
// Delete the user
restClient.deleteUser(createdUser.id());
System.out.println("User Deleted Successfully");
System.out.println("------ REST Client Test Completed ------");
}
}
π Explanation
βοΈ CommandLineRunner
executes the REST Client when the application starts.
βοΈ Calls all CRUD methods and prints responses in the console.
π Step 5: Run and Test the Application
π Start the Spring Boot application in IDE or use the following maven command:
mvn spring-boot:run
π Expected Console Output
π Congratulations! You have successfully built and tested a REST Client in Spring Boot! π―
π― Summary: What We Achieved
βοΈ Created a WebClient-based REST Client to test APIs.
βοΈ Automated API testing instead of using Postman.
βοΈ Executed CRUD operations using our custom client.
βοΈ Printed the API responses to the console.
π Now you can use this REST Client to test any Spring Boot REST API! ππ₯