DEV Community

realNameHidden
realNameHidden

Posted on

1 1 1 1 1

how to use RestTemplate in a Spring Boot application to make an HTTP GET request

✅ What is RestTemplate?
RestTemplate is a synchronous client to perform HTTP requests in a Spring application (mostly used before WebClient).

🔧 Example: Using RestTemplate to Call an External API
💡 Scenario: Call a public API that returns user data (like https://jsonplaceholder.typicode.com/users/1)
Create the Spring Boot project Using Spring web Dependency

Directory Structure :

Image description

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.4.5</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>RestTemplateExample</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>RestTemplateExample</name>
 <description>Demo project for Spring Boot</description>
 <url/>
 <licenses>
  <license/>
 </licenses>
 <developers>
  <developer/>
 </developers>
 <scm>
  <connection/>
  <developerConnection/>
  <tag/>
  <url/>
 </scm>
 <properties>
  <java.version>17</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>
Enter fullscreen mode Exit fullscreen mode

🔹 Step 1: Add RestTemplate Bean in Configuration

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

 @Bean
 public RestTemplate restTemplate() {
  return new RestTemplate();
 }
}

Enter fullscreen mode Exit fullscreen mode

🔹 Step 2: Create a DTO for the Response (Optional but good practice)

package com.example.demo.model;

public class User {

 private int id;
 private String name;

 private String username;

 private String email;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 @Override
 public String toString() {
  return "User [id=" + id + ", name=" + name + ", username=" + username + ", email=" + email + "]";
 }

}

Enter fullscreen mode Exit fullscreen mode

🔹 Step 3: Use RestTemplate in a Service or Controller

package com.example.demo.controller;

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

import com.example.demo.model.User;

@RestController
public class ExternalApiController {

 @Autowired
 private RestTemplate restTemplate;


 @GetMapping("/user")
 public User getUser() {
        String url = "https://jsonplaceholder.typicode.com/users/1";
        return restTemplate.getForObject(url, User.class);
 }
}

Enter fullscreen mode Exit fullscreen mode

✅ Output (JSON response):

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "Sincere@april.biz"
}

Enter fullscreen mode Exit fullscreen mode

ACI image

ACI.dev: Best Open-Source Composio Alternative (AI Agent Tooling)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Star our GitHub!

Top comments (1)

Collapse
 
drezir profile image
Adam Ostrožlík

Dont write posts about RestTemplate, which is already deprecated. Write posts about RestClient.