To consume the exposed Restfull web services in Spring Application, Rest Template is also one of the popular library or helper class available in spring framework. This example is ellustrated with how to consume the restfull api by using Rest Template
class.
Rest Template class will provide many helper methods to consume the Restfull webservices like
- getForEntity
- postForEntity
- put
- delete
If you dont want to use different methods for each of the API means, you can go for the single function exchange
and pass the specific Http Method type to consume API's.
Before using the Rest Client , you need to initailize, that can be done in any of your class as below,
You can then access application by using Url here: http://localhost:8080/
Our issue tracker is available here: https://github.com/Sudarshan-Gowda/Spring-Boot-Rest-Template/issues
The following items should be installed in your system:
- STS - 3.0 Plus
- Postman or Any Rest API Testing tool
- Download this Project and do maven import.
git clone https://github.com/Sudarshan-Gowda/Spring-Boot-Rest-Template.git
- To Import the Praject Using STS or Eclipse
File -> Import -> Maven -> Existing Maven project
Spring Boot Configuration | Class or Java property files |
---|---|
Main Class - Expose | ExposeController |
Main Class - Consume | ConsumeController |
- Run the Application by using CLI,
mvn spring-boot:run
- Test the API by using Rest API Testing tool.
### Find the exposed restfull APIS as below
Place the below urls and select appropicate Http Methods to test the APIs
a. To get the list of employees
http://localhost:8080/employees
b. To fetch the employee by id
http://localhost:8080/employees/1
c. To create the employee record
http://localhost:8080/employees
{ "empId": 1, "empName": "Sudarshan", "empDept": "PD", "empSalary": 1234567 }
d. To update the employee record
http://localhost:8080/employees
{ "empId": 1, "empName": "Sudarshan", "empDept": "PD", "empSalary": 1234567 }
e. To delete the employee record
http://localhost:8080/template/employees/1
a. To get the list of employees
http://localhost:8080/template/employees
b. To fetch the employee by id
http://localhost:8080/template/employees/1
c. To create the employee record
http://localhost:8080/template/employees
{ "empId": 1, "empName": "Sudarshan", "empDept": "PD", "empSalary": 1234567 }
d. To update the employee record
http://localhost:8080/template/employees
{ "empId": 1, "empName": "Sudarshan", "empDept": "PD", "empSalary": 1234567 }
e. To delete the employee record
http://localhost:8080/template/employees/1
The issue tracker is the preferred channel for bug reports, features requests and submitting pull requests.
For pull requests, editor preferences are available in the editor config for easy use in common text editors.