Spring Restful client – RestTemplate example

In this tutorial, we will see how to create rest client using Spring RestTemplate.

We have already seen Spring restful web services crud example. We have used postman utility to demonstrate all HTTP methods such as get, post, delete and put but if you want to write java code for restful client , you can use Spring RestTemplate.

You can always use java ‘s HttpClient but Spring RestTemplate provides more methods and options that you can use to consume Spring restful web services via Http methods.

Here is list of methods provided by Spring Resttemplate for each http methods.

Method
Spring RestTemplate’s method
Get
 getForObject, getForEntity
Post
postForObject(String url, Object request, Class responseType, String…​ uriVariables)
postForLocation(String url, Object request, String…​ urlVariables),
Put
put(String url, Object request, String…​urlVariables)
Delete
delete()
Head
headForHeaders(String url, String…​ urlVariables)
Options
optionsForAllow(String url, String…​ urlVariables)

I am using same example which we have seen in Spring rest crud example. I am using java client instead of postman to consure Rest APIs.

Get example:

You can use getForObject or getForEntity for calling http get method.

Spring Rest API Code get method:

Spring RestTemplate get Method :

When you run above code, you will get below output:

Post example :

You can use PostForObject or PostForLocation to call post method.

Spring Rest API Code post method:

Spring RestTemplate post Method :

When you run above code, you will get below output:

Put example :

You can use put to call http put method.

Spring Rest API Code put method:

Spring RestTemplate put Method :

When you run above code, population for bhutan will be updated to 10000.

Delete example :

You can use delete to call http delete method.

Spring Rest API Code delete method:

Spring RestTemplate delete Method :

When you run above code, bhutan will be deleted from list of countries.

You may also like:

 


import_contacts

You may also like:

Related Posts

  • 29 April

    Spring boot rest example

    In this tutorial, we will see how to create Restful web services using Spring boot. There are not many differences between creating Rest API using Spring Rest JSON which we have already seen earlier. Spring Boot just makes easier to Rest API. You don’t have to provide any additional JSON dependency in the classpath. Spring […]

  • 14 September

    Spring Rest + Spring Security example

    In this post , we are going to apply Spring Security on Spring Rest example. Web services tutorial: Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference between SOAP and REST web services SOAP web service example in java using eclipse JAX-WS web service eclipse tutorial JAX-WS […]

  • 19 August

    Spring @RestController example

    In this post,  we are going to see another important annotation called @RestController. It is generally used while working with Spring restful Web services implementation. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller […]

  • 17 August

    Spring Rest Hibernate example

    In previous post, we have already seen Spring Rest crud example . In this post, we will extend same example and integrate it with hibernate and mysql. We will use following annotations for CRUD operation. Method Description Get It is used to read resource Post It is used to create new resource. It is not […]

  • 08 August

    Spring MVC @RequestMapping annotation example

    We have already seen Spring MVC hello world example. @RequestMapping is one of important annotation which you use in Spring MVC. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller example Spring MVC @ModelAttribute annotation […]

  • 06 August

    Spring MVC angularjs example

    n this tutorial, we will see Spring MVC angularjs example. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller example Spring MVC @ModelAttribute annotation example Spring MVC @RestController annotation example Spring MultiActionController Example Spring […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.