<div dir="ltr" style="text-align: justify;">
<p>In Spring REST client, The RestTemplate is the core class for client-side access to Spring RESTful web services. It communicates HTTP server using <a href="https://dineshonjava.com/2016/10/what-is-rest-and-rest-architecture-and-rest-constraints.html" target="_blank" rel="noopener"><b>RESTful constraints</b></a>. It is very similar to other template classes in the Spring like JdbcTemplate and HibernateTemplate etc. In Spring, RestTemplate provides higher level implementation of corresponding HTTP methods such as GET, POST, PUT, DELETE, HEAD etc. It provides the methods to communicate by using these HTTP methods with URI template, URI param, request object and response type as arguments.</p>
<div id="ads-id" align="center"></div>
<p>In this example, we will see how to consume JSON response. In my previous article we have seen <a href="https://dineshonjava.com/2017/03/spring-restful-web-services-json-crud-example.html" target="_blank" rel="noopener"><b>Spring RESTful web services crud example</b></a>. Earlier we have used postman REST UI based client to demonstrate all HTTP methods such as get, post, delete and put. But here we are going to consume Restful web services via RestTemplate of Spring REST client.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>RestTemplate Methods</b><br />
RestTemplate provides higher level methods that correspond to each of the six main HTTP methods that make invoking many RESTful services. In the following list has methods provided by Spring RestTemplate for each http methods.</p>
<table>
<tbody>
<tr style="height: 12.9pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;">
<td style="border: 1pt solid; height: 12.9pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;"><b>Method</b></div>
</td>
<td style="border: 1pt solid; height: 12.9pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;"><b>Spring RestTemplate’s method</b></div>
</td>
</tr>
<tr style="height: 38.05pt; mso-yfti-irow: 1;">
<td style="border: 1pt solid; height: 38.05pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Get</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 38.05pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;"> getForObject, getForEntity</div>
</td>
</tr>
<tr style="height: 51.7pt; mso-yfti-irow: 2;">
<td style="border: 1pt solid; height: 51.7pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Post</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 51.7pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">
<p>postForObject(String url, Object request, Class responseType, String…? uriVariables)</p>
<p>postForLocation(String url, Object request, String…? urlVariables),</p>
</div>
</td>
</tr>
<tr style="height: 25.85pt; mso-yfti-irow: 3;">
<td style="border: 1pt solid; height: 25.85pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Put</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 25.85pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">put(String url, Object request, String…?urlVariables)</div>
</td>
</tr>
<tr style="height: 25.15pt; mso-yfti-irow: 4; mso-yfti-lastrow: yes;">
<td style="border: 1pt solid; height: 25.15pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Delete</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 25.15pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">delete()</div>
</td>
</tr>
<tr style="height: 51.7pt; mso-yfti-irow: 2;">
<td style="border: 1pt solid; height: 51.7pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Head</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 51.7pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">headForHeaders(String url, String…? urlVariables)</div>
</td>
</tr>
<tr style="height: 25.85pt; mso-yfti-irow: 3;">
<td style="border: 1pt solid; height: 25.85pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">Options</div>
</td>
<td style="border-bottom-width: 1pt; border-color: initial; border-right-width: 1pt; border-style: none solid solid none; height: 25.85pt; padding: 0cm 5.4pt; width: 159.1pt;" valign="top"> ;</p>
<div style="margin-bottom: 0.0001pt;">optionsForAllow(String url, String…? urlVariables)</div>
</td>
</tr>
</tbody>
</table>
<p>As per as name of these methods of RestTemplate class indicate which HTTP method they implement internally and second part of the method name indicates what is return by these methods. For example, getForObject() will perform a HTTP GET action on the server, and convert the HTTP response into an object of given type and return it to the client. And other method postForLocation() will do a HTTP POST action on the server, converting the given object into a HTTP request, and returns the response HTTP Location header where the newly created object can be found.</p>
<p><b>Let&#8217;s see these RestTemplate&#8217;s methods with examples.</b><br />
Here I am using<b> <a href="https://dineshonjava.com/2017/03/spring-restful-web-services-json-crud-example.html">Spring Restful web services CRUD JSON Example</a></b> of my previous post.</p>
<p>In this example, we are using <b><i>@GetMapping</i></b>, <b><i>@PostMapping</i></b>, <i><b>@PutMapping</b> </i>and <b><i>@DeleteMapping</i></b> annotations, these annotations are introduced as of Spring 4.3 version in parallel of <b><i>@RequestMapping</i></b> annotation with Http Methods as below.</p>
<pre class="highlight">@GetMapping = @RequestMapping + Http GET method 
@PostMapping = @RequestMapping + Http POST method 
@PutMapping = @RequestMapping + Http PUT method 
@DeleteMapping = @RequestMapping + Http DELETE method 
</pre>
<p><b>HTTP GET Method Example</b><br />
For API GET method call we can use either getForObject() or getForEntity() method of RestTemplate.</p>
<p><b>Spring REST API GET CODE:</b></p>
<pre class="highlight">@GetMapping("/account/{accountId}") 
public Account get (@PathVariable Long accountId){ 
 return accountService.get(accountId); 
} 
</pre>
<p><b>We could define same above API call as below:</b></p>
<pre class="highlight">@RequestMapping(method=RequestMethod.GET, value = "/account/{accountId}", produces = MediaType.APPLICATION_JSON_VALUE) 
public Account get (@PathVariable Long accountId){ 
 return accountService.get(accountId); 
} 
</pre>
<p>This call binds to convert on JSON media type only with the @RequestMapping annotation. If you want flexible return media type either JSON or XML based on the message converter available on the classpath of the application then it&#8217;s recommend do not define produces property request mapping annotation.</p>
<p><b>Spring RestTemplate get Method :</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.restclient.account; 
 
import org.springframework.web.client.RestTemplate; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SpringRestClient { 
 
 private static RestTemplate restTemplate = new RestTemplate(); 
 private static final String baseURL = "http://localhost:8080/restapi/"; 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 //Read Account details for a given accountId = 1 using GET method of RestTemplate 
 Account accountDetail = restTemplate.getForObject(baseURL+"account/1", Account.class); 
 System.out.println("Account Detail for given AccountId : " +accountDetail); 
 
 } 
 
} 
 
</pre>
<p><b>Output on console:</b><br />
After running above code, you will get below output:</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">Account Detail for given AccountId : Account [accountId=1, name=Dinesh Rajput, city=Noida, balance=212.33]</div>
<p><b>HTTP POST Method Example</b></p>
<p>For Http POST method calling we can use either <b><i>postForObject()</i></b> or <b><i>postForLocation()</i></b> method of RestTemplate.</p>
<p><b>Spring Rest API Code post method:</b></p>
<pre class="highlight">@PostMapping("/account") 
public Account create (@RequestBody Account account){ 
 return accountService.create(account); 
} 
</pre>
<p><b>Spring RestTemplate post Method call:</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.restclient.account; 
 
import org.springframework.web.client.RestTemplate; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SpringRestClient { 
 
 private static RestTemplate restTemplate = new RestTemplate(); 
 private static final String baseURL = "http://localhost:8080/restapi/"; 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 //Create Account using POST method of RestTemplate 
 Account account = new Account("Arnav Rajput", "Noida", 312.33); 
 account = restTemplate.postForObject(baseURL+"account", account, Account.class); 
 System.out.println("Added account: " +account); 
 
 } 
 
} 
 
</pre>
<p><b>Output on console:</b><br />
After running above code, you will get below output:</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">Added account: Account [accountId=3, name=Arnav Rajput, city=Noida, balance=312.33]</div>
<p><b>HTTP PUT Method Example</b></p>
<p>For Http PUT method calling we can use put() method of RestTemplate.</p>
<p><b>Spring Rest API Code put method:</b></p>
<pre class="highlight">@PutMapping("/account") 
public Account update (@RequestBody Account account){ 
 return accountService.update(account); 
} 
</pre>
<p><b>Spring RestTemplate put Method call:</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.restclient.account; 
 
import org.springframework.web.client.RestTemplate; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SpringRestClient { 
 
 private static RestTemplate restTemplate = new RestTemplate(); 
 private static final String baseURL = "http://localhost:8080/restapi/"; 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 //Create Account using POST method of RestTemplate 
 Account account = new Account("Arnav Rajput", "Noida", 312.33); 
 account = restTemplate.postForObject(baseURL+"account", account, Account.class); 
 System.out.println("Added account: " +account); 
 
 <b>//Update Account detail for given accountId = 1 using PUT method of RestTemplate 
 Account updateAccount = new Account("Anamika Rajput", "Noida", 123.33); 
 updateAccount.setAccountId(1l); 
 restTemplate.put(baseURL+"account", updateAccount); 
 Account updatedAccount = restTemplate.getForObject(baseURL+"account/1", Account.class); 
 System.out.println("Updated Account: " +updatedAccount);</b> 
 
 } 
 
} 
 
</pre>
<p><b>Output on console:</b><br />
After running above code, you will get below output:</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">Added account: Account [accountId=1, name=Arnav Rajput, city=Noida, balance=312.33]<br />
<b>Updated Account: Account [accountId=1, name=Anamika Rajput, city=Noida, balance=123.33]</b></div>
<p><b>HTTP DELETE Method Example</b></p>
<p>For Http DELETE method calling we can use delete() method of RestTemplate.</p>
<p><b>Spring Rest API Code delete method:</b></p>
<pre class="highlight">@DeleteMapping("/account/{accountId}") 
public void delete (@PathVariable Long accountId){ 
 accountService.delete(accountId); 
} 
</pre>
<p><b>Spring RestTemplate delete Method call:</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.restclient.account; 
 
import java.util.List; 
 
import org.springframework.web.client.RestTemplate; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SpringRestClient { 
 
 private static RestTemplate restTemplate = new RestTemplate(); 
 private static final String baseURL = "http://localhost:8080/restapi/"; 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 List<;Account>; accounts = restTemplate.getForObject(baseURL+"accounts", List.class); 
 System.out.println("Total Accounts before DELETE call: "); 
 for(Object acct : accounts){ 
 System.out.println(acct); 
 } 
 
 //Delete Account for given accountId = 2 using Delete method of RestTemplate 
 restTemplate.delete(baseURL+"account/2"); 
 accounts = restTemplate.getForObject(baseURL+"accounts", List.class); 
 System.out.println("Total Accounts after DELETE call: "); 
 for(Object acct : accounts){ 
 System.out.println(acct); 
 } 
 } 
 
} 
</pre>
<p><b>Output on console:</b><br />
After running above code, you will get below output:</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Total Accounts before DELETE call: </b><br />
{accountId=1, name=Anamika Rajput, city=Noida, balance=123.33}<br />
{accountId=2, name=Anamika Rajput, city=Noida, balance=222.33}<br />
{accountId=3, name=Arnav Rajput, city=Noida, balance=522.33}<br />
<b>Total Accounts after DELETE call: </b><br />
{accountId=1, name=Anamika Rajput, city=Noida, balance=123.33}<br />
{accountId=3, name=Arnav Rajput, city=Noida, balance=522.33}</div>
<p><b>Download Source Code from GitHub.</b></p>
<ol style="text-align: left;">
<li><a href="https://github.com/dineshonjava/SpringREST-CRUD-Example" target="_blank" rel="noopener"><b>Spring RESTful web service CRUD Example</b></a></li>
<li><a href="https://github.com/dineshonjava/SpringREST-Client-RestTemplate" target="_blank" rel="noopener"><b>Spring REST Client using RestTemplate Example</b></a></li>
</ol>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/03/Spring-Rest-client-RestTemplate-Example.jpg" border="0" /></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-restful-web-services-json-crud-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-restful-web-services-xml-crud-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '40'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…