Spring is a standout amongst the most broadly utilized Java EE system. We have prior perceived how to utilize Spring MVC to make Java based web applications. Today we will figure out how to make Spring Restful Web Services utilizing Spring MVC and afterward test it out with the Rest client. Toward the end, we will likewise investigate how to conjure Spring Restful web benefit utilizing Spring RestTemplate API.
So lets started step by step Spring Boot RESTful web services example:
Step 1. Create the RESTful Spring Boot project
Open your STS(Spring Tool Suite).Goto File - Click on NEW - Spring Starter Project - Give Name Demo (As show in following image 1) - click on next - search for MYSQL Dependencies(as Show in Following image 2) select it and click on Finish.
|
Spring Boot RESTful web services example Image 1 |
|
Spring Boot RESTful web services example Image 2 |
Wait for few minutes it install all required library.
Step 2.Go to application.properties
Add the following properties in application.properties.
|
Spring Boot RESTful web services example Image 3 |
This will help us to insert ,update delete and get all records from mysql tables.You also can use different Database.Here we are using MYSQL to perfrom Rest API Opertions like POST,GET method.
Step 3. Create Java Package
Create Java Package name called Topics. click on src/main/java -New -Package Give name Topics
as Shown in Following Image 4
|
Spring Boot RESTful web services example Image 4 |
Step 4. Create Class name Topics.java
Create class name Topics.java in com.example.demo.Topics Package. Add some Entity to it and create getter and setter for all Entity. as shown in following image 5
|
Spring Boot RESTful web services example Image 5 |
Step 5. Create Interface Name TopicsRepository
Create Interface called TopicsRepository as show in following Image 6.Which extends CrudRepository. This Repository is used to perform MYSQL Oprations for Topics Entity.
|
Spring Boot RESTful web services example Image 6 |
Step 6. Create Class name TopicsService
In this class we add some method that will help us to perform MYSQL CRUD opertions. We are used anotation called @services and @autowired. as showin following image 7 of TopicsServices.java
|
Spring Boot RESTful web services example Image 7 |
Step 7. Run The Project
Final Step is run project and tests the Rest API. Run the Project as Spring Boot App.
And Open POSTMAN OR Advance Rest Client to test the our API Working or not.
You can Following Images to see the API test result and MYSQL Table Details.
|
Spring Boot RESTful web services example Image 8 |
|
Spring Boot RESTful web services example Image 9 |
|
Spring Boot RESTful web services example Image 10 |
So This is it Follow all the step above and you get idea about how to write Rest api using spring boot.
Spring Boot RESTful web services example tell me you like it not comment us below.Thank you.
ConversionConversion EmoticonEmoticon