You are currently viewing A Guide to RestClient in Spring Boot

A Guide to RestClient in Spring Boot

RESTful web services have become a standard for communication between web servers and clients. In Spring Boot, consuming these RESTful services is simplified with the RestTemplate class. In this tutorial, we’ll explore how to use RestTemplate as a RestClient in a Spring Boot application.

What is RestClient?

A RestClient is a client-side application or library that makes HTTP requests to a RESTful web service. In the context of Spring Boot, the RestTemplate class serves as a powerful tool for RestClient operations. It simplifies the process of consuming RESTful services by abstracting away the complexity of HTTP requests and responses.

Setting up a Spring Boot Project

First, make sure you have a Spring Boot project set up. If you don’t, you can quickly create one using the Spring Initializr. Include the “Spring Web” dependency, as we’ll be using RestTemplate, which is part of this module.

Adding Dependencies

In your pom.xml (if using Maven) or build.gradle (if using Gradle), add the following dependency for RestTemplate:




implementation 'org.springframework.boot:spring-boot-starter-web'

Creating a RestClient

Let’s create a simple RestClient to consume a RESTful API that provides information about users. For demonstration purposes, we’ll use a public API from JSONPlaceholder, which provides fake data for testing and prototyping.

First, create a User class to represent the structure of the user data we’ll be retrieving:

public class User {
    private Long id;
    private String name;
    private String username;
    private String email;

    // getters and setters

Next, create a UserService class that will handle the RestClient operations using RestTemplate:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

public class UserService {

    private final RestTemplate restTemplate;
    private final String baseUrl;

    public UserService(RestTemplate restTemplate, @Value("${api.base.url}") String baseUrl) {
        this.restTemplate = restTemplate;
        this.baseUrl = baseUrl;

    public User getUserById(Long id) {
        return restTemplate.getForObject(baseUrl + "/users/" + id, User.class);

In this class:

  • RestTemplate is autowired into the constructor.
  • We use @Value to inject the base URL of the API. You can define this in your or application.yml file.
  • getUserById method makes a GET request to retrieve a user by ID.

In your src/main/resources/, define the base URL of the API:


Finally, in your main Spring Boot application class, you’ll need to create a RestTemplate bean:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

public class Application {

    public static void main(String[] args) {, args);

    public RestTemplate restTemplate() {
        return new RestTemplate();

Using the RestClient

Now that we have our UserService set up, we can use it in our controller to fetch user data.

Create a UserController to expose an endpoint for retrieving user data:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;

    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);

Testing the Application

Now, when you run your Spring Boot application, you can make a GET request to http://localhost:8080/users/1 (or any other user ID) to retrieve user data from the JSONPlaceholder API.

For example, visiting http://localhost:8080/users/1 should return JSON data similar to:

  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": ""


In this tutorial, we’ve explored how to create a simple RestClient using RestTemplate in a Spring Boot application. We defined a User class to represent the data structure, created a UserService to handle RestClient operations, and exposed a UserController to retrieve user data via a RESTful endpoint.

Using RestTemplate simplifies the process of consuming RESTful services by handling the complexities of HTTP requests and responses. This example demonstrates a basic implementation, but RestTemplate offers a wide range of methods for handling different types of requests, headers, and responses. Experiment with different endpoints and explore more advanced features to leverage the full power of RestTemplate in your Spring Boot applications.

Leave a Reply