How to use Spring RestTemplate client for consuming Restful WebService

In the tutorial, JavaSampleApproach introduces you a central class RestTemplate that Spring provides for simplified handles restful connection and parsing results.

Related post: How to use Spring RestTemplate client for consuming Restful WebService

I. Technology for Spring RestTemplate tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview

In the tutorial, we create 2 projects, once for restful services, another for rest-template client

1. Project Structure

spring resttemplate project structure

2. Step to do

– Create a Spring Boot project for Restful WebServices & Spring RestTemplate Client
– Create a business model class
– For Restful Web Services, create Controllers: POST, GET, PUT, DELETE
– For Rest-Template client, create Spring Rest-Template with functions: POST, GET, PUT, DELETE
– Apply Rest-Template client
– Build & Enjoys with results

III. Practices
1. Create a Spring Boot project for Restful WebServices

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, then input needed project info. Press Next, then press Finish, a Spring Boot project will be created successfully.

Open pom.xml file, add Web dependencies:

2. Create a business model class

3. For Restful Web Services, create Controllers: POST, GET, PUT, DELETE

– Create a RestService class:

– Create a POST request mapping

– Create a GET request mapping

– Create a PUT request mapping

– Create a DELETE request mapping

4. For Rest-Template client, create Spring Rest-Template with functions: POST, GET, PUT, DELETE

– Create a RestfulService class:

– With POST request mapping of restful-service, create a POST function:

– With GET request mapping of restful-service, create a GET function at rest-template client:

– With PUT request mapping of restful service, create a PUT function at rest-template client

– With DELETE request mapping of resful service, create a DELETE function at rest-template client:

Note: replace localhost:8080 by your restful-services’s host & port

5. Apply Rest-Template client

Use Rest-Template Client has built for communicating with Restful-Services

6. Build & Enjoys with results

– Build 2 projects: Spring Restful Service and Spring RestTemplate Client.
– Order Run:
1. Run Spring Restful Service
2. Run Spring RestTemplate Client

Result:
1. Logs from Spring Restful Service:

2. Logs from Spring RestTemplate Client:

IV. Sourcecode

springboot-restful-service
springboot-resttemplate


Related Posts



Got Something To Say:

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

*