How to create GET ALL and GET ONE record API in the spring boot application? Simple GET API in Spring Boot.
Hello friends,
Today we are going to build two simple GET APIs using Spring Boot Application. One API is for fetching All the available records and the Second will fetch only one record by using his ID.
For that, we need some software and tools.
Any IDE ( I am using IntelliJ IDEA Community Edition 2021.2.3) you can use VS Code, Eclipse, STS, etc.
JAVA JDK 8 or above, If you don't have JDK installed please go to this article here I have described in detail for Download and install JDK/JRE Setup environment variable and JDK directory structures
POSTMAN - This will use to test our REST APIs
I have provided all the download links here you just click on the name page automatic forwarded you to the respective download page.
I assume that you have installed all the necessary required software and tools, Let's begin with our today's tutorial titled - How to create simple GET APIs in spring boot application.
First, we need a spring boot template for that we can go to the Spring Initializr website https://start.spring.io/
Spring Initializr |
Artifact - Give your App Name - BookManagementApp.
Name - will automatically populate in the text box.
Description - Give it any.
Packaging - Jar
Java version select according to you which you have installed I have 11 so I am selecting 11.
Now its time to select dependencies - You can select the following shown in Screen Shot >>
Select Spring boot dependencies |
2) Spring Boot Dev Tools
Don't worry about dependencies we can add later as well.
Now click on GENERATE button this will download one zip file which has all the configuration files in it. Just unzip the file and open using your IDE.
NOTE- Make sure Your PC/Laptop has an active internet connection for downloading and configuring all the necessary files for the spring boot project (while opening your Spring Boot Project)
After completing all the processes you will see your project like this.
IntelliJ IDEA IDE |
Click on the + (plus) icon and select Application from there.
After that give the name as your project name. and Select Java Version with main class in Build and run section.
Give Name and Select JAVA Version |
Select Main Class |
Copy that using just the above the copy button.
Copy Path (JAVA) variable |
Paste the above box with the help of the paste button.
Paste Path variable |
Click on Apply Ok and save the project. This will save all the changes you made and be ready to run your spring boot project.
--------------------------------------------------------------------------------------------------------------------
Now create 3 folders under your package
Right-click on Package name Goto new and select Package.
Creating a new Java Package folder |
Name for a new package |
1) controllers
2) entities or models
3) services
New Package folders |
1) Controllers class files -
2) Entities or Model class files -
In the Sprint Boot, the Model or Entities class files are those files where we can create Getters, Seters, and Constructers. This is also called POJO class, POJO means Pain Old Java Objects class file.
3) Services class files -
The Service sections are a class file containing the @Service annotation. These class files are used to write business ideas in a separate layer, separated from the class file @RestController.
--------------------------------------------------------------------------------------------------------------------------
Open the Entities Package folder and create a new java class called Book.java
Click on Package folder you want to create new class file >> New >> Java Class >> Give name >> Select Class >> Hit enter.
New File |
New Class |
Code -
---------------------------------------------------------------------------------------------------------------------------
Open the Controllers Package folder and create a new java class called BookController.java
Code -
-----------------------------------------------------------------------------------------------------------------------------
Open the Services Package folder and create a new java class called BookService.java
Code -
-----------------------------------------------------------------------------------------------------------------------------
Now are all set to run our Application -
Run Application |
--------------------------------------------------------------------------------------------------------------------------
Open your POSTMAN Application to test our API -
By default, Spring Boot Application runs on localhost:8080 port.
1) GET ALL BOOKS
URL- localhost:8080/books
POSTMAN - GET ALL BOOKS |
2.1) GET Book by ID 01
URL - localhost:8080/books/01
POSTMAN - GET BOOK by ID |
GET Book by ID 05 -
URL - localhost:8080/books/05
POSTMAN - GET BOOK by ID |
NOTE - OR you can use the same URL in any web browser to test your GET API.
For the upcoming tutorial we learn about POST, PUT, DELETE means Create, Update, Delete Operation in Spring Boot Application. If you like this tutorial please share it with your friends who want to learn about Spring Boot APIs.
GitHub Link for this commit -
Commit Number - 087f7c3610504091152c8d87cb52ff4b536599c3
GIT Url - https://github.com/amolgadge663/BookManagementApp_SpringBoot_Backend
1 Comments
Great explaination !!
ReplyDelete