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 -
package com.amolsoftwares.BookManagementApp.entities; | |
public class Book { | |
private int id; | |
private String name; | |
private String author; | |
public Book(int id, String name, String author) { | |
this.id = id; | |
this.name = name; | |
this.author = author; | |
} | |
public Book(){ | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
@Override | |
public String toString() { | |
return "Book{" + | |
"id=" + id + | |
", name='" + name + '\'' + | |
", author='" + author + '\'' + | |
'}'; | |
} | |
} |
---------------------------------------------------------------------------------------------------------------------------
Open the Controllers Package folder and create a new java class called BookController.java
Code -
package com.amolsoftwares.BookManagementApp.controllers; | |
import com.amolsoftwares.BookManagementApp.entities.Book; | |
import com.amolsoftwares.BookManagementApp.services.BookService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.List; | |
@RestController | |
public class BookController { | |
@Autowired | |
public BookService bookService; | |
@GetMapping("/books") | |
private List<Book> getBooks(){ | |
return bookService.getAllBooks(); | |
} | |
@GetMapping("/books/{id}") | |
private Book getBookById(@PathVariable("id") int id){ | |
return bookService.getBookById(id); | |
} | |
} |
-----------------------------------------------------------------------------------------------------------------------------
Open the Services Package folder and create a new java class called BookService.java
Code -
package com.amolsoftwares.BookManagementApp.services; | |
import com.amolsoftwares.BookManagementApp.entities.Book; | |
import org.springframework.stereotype.Service; | |
import java.util.ArrayList; | |
import java.util.List; | |
@Service | |
public class BookService { | |
private static List<Book> list = new ArrayList<>(); | |
static { | |
list.add(new Book(01,"Book Name 01", "Book Author Name 01")); | |
list.add(new Book(02,"Book Name 02", "Book Author Name 02")); | |
list.add(new Book(03,"Book Name 03", "Book Author Name 03")); | |
list.add(new Book(04,"Book Name 04", "Book Author Name 04")); | |
list.add(new Book(05,"Book Name 05", "Book Author Name 05")); | |
} | |
//get all books | |
public List<Book> getAllBooks(){ | |
return list; | |
} | |
//get single book by its it | |
public Book getBookById(int id){ | |
Book book = null; | |
book = list.stream().filter(e -> e.getId() == id).findFirst().get(); | |
return book; | |
} | |
} |
-----------------------------------------------------------------------------------------------------------------------------
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