Spring Boot - Rest Template
Spring Boot - Rest Template
Rest Template is used to create applications that consume RESTful Web Services. You can use
the exchange() method to consume the web services for all HTTP methods. The code given below
shows how to create Bean for Rest Template to auto wiring the Rest Template object.
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
@Bean
GET
Consuming the GET API by using RestTemplate - exchange() method
"id": "1",
"name": "Honey"
},
"id": "2",
"name": "Almond"
You will have to follow the given points to consume the API −
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm 1/8
7/31/22, 7:47 PM Spring Boot - Rest Template
@RestController
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/template/products")
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange("
POST
Consuming POST API by using RestTemplate - exchange() method
Assume this URL http://localhost:8080/products returns the response shown below, we are
going to consume this API response by using the Rest Template.
"id":"3",
"name":"Ginger"
You will have to follow the points given below to consume the API −
Provide the URL, HttpMethod, and Return type for exchange() method.
@RestController
@Autowired
RestTemplate restTemplate;
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm 2/8
7/31/22, 7:47 PM Spring Boot - Rest Template
return restTemplate.exchange(
PUT
Consuming PUT API by using RestTemplate - exchange() method
"name":"Indian Ginger"
You will have to follow the points given below to consume the API −
Provide the URL, HttpMethod, and Return type for exchange() method.
@RestController
@Autowired
RestTemplate restTemplate;
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
DELETE
Consuming DELETE API by using RestTemplate - exchange() method
You will have to follow the points shown below to consume the API −
Provide the URL, HttpMethod, and Return type for exchange() method.
@RestController
@Autowired
RestTemplate restTemplate;
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
package com.tutorialspoint.demo.controller;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.tutorialspoint.demo.model.Product;
@RestController
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/template/products")
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
return restTemplate.exchange(
The code for Spring Boot Application Class – DemoApplication.java is given below −
package com.tutorialspoint.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
SpringApplication.run(DemoApplication.class, args);
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm 5/8
7/31/22, 7:47 PM Spring Boot - Rest Template
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</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>
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
repositories {
mavenCentral()
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm 6/8
7/31/22, 7:47 PM Spring Boot - Rest Template
group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
You can create an executable JAR file, and run the Spring Boot application by using the following
Maven or Gradle commands −
After “BUILD SUCCESS”, you can find the JAR file under the target directory.
After “BUILD SUCCESSFUL”, you can find the JAR file under build/libs directory.
Now hit the below URL’s in POSTMAN application and you can see the output.
https://www.tutorialspoint.com/spring_boot/spring_boot_rest_template.htm 8/8