Spring Boot - REST Example
Last Updated :
05 Sep, 2025
Modern web applications follow the Client-Server Architecture, where the frontend (client) communicates with the backend (server) using the HTTP protocol. Servers expose services (APIs) that clients can call via HTTP requests. REST (Representational State Transfer) is a set of architectural principles for building these services.
REST uses standard HTTP methods to perform CRUD operations:
- POST-> Create
- GET -> Read
- PUT -> Update
- DELETE -> Delete
Step-by-Step Guide to Build a RESTful API using SpringBoot
1. Create the Spring Boot project
Using STS (Spring Tool Suite / Eclipse):
- File -> New -> Spring Starter Project -> Next.
- Enter Name: book-api (Group com.example, Artifact book-api), Java 17, Maven.
- Add dependencies: Spring Web, Spring Boot DevTools.
- Click Finish.
Step 2: Project Structure
Once the project is created, we will see the following structure
pom.xml:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>book-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>book-api</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Note:
Since we are not using a database in this example, no configuration is needed in application.properties.
Step 3: Create the POJO Class
A POJO class represents the data model. In this case, we’ll create a Book class.
Book.java:
Java
package com.example.demo.model;
public class Book {
private int id;
private String title;
private String author;
public Book(int id, String title, String author)
{
this.id = id;
this.title = title;
this.author = author;
}
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title)
{
this.title = title;
}
public String getAuthor() { return author; }
public void setAuthor(String author)
{
this.author = author;
}
}
Step 4: Create the Service Interface and Service Implementation Class
Here, we have created an interface called BookService which contains all the service methods that our application is going to provide to the user. And BookServiceImpl class that implements the BookService interface.
Java
package com.example.demo.service;
import com.example.demo.model.Book;
import java.util.List;
public interface BookService {
List<Book> findAllBooks();
Book findBookById(int id);
void deleteAllBooks();
}
Service Implementation Class
Java
package com.example.demo.service;
import com.example.demo.model.Book;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
private List<Book> books = new ArrayList<>();
public BookServiceImpl() {
// Sample data for books
books.add(new Book(1, "The Great Gatsby", "F. Scott Fitzgerald"));
books.add(new Book(2, "1984", "George Orwell"));
books.add(new Book(3, "To Kill a Mockingbird", "Harper Lee"));
}
@Override
public List<Book> findAllBooks() {
return books;
}
@Override
public Book findBookById(int id) {
return books.stream().filter(book -> book.getId() == id).findFirst().orElse(null);
}
@Override
public void deleteAllBooks() {
books.clear();
}
}
Step 5: Create the REST Controller
The Controller layer exposes the APIs to the client. Create a RestController class to handle HTTP requests.
ExController.java:
Java
package com.example.demo.controller;
import com.example.demo.model.Book;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api")
public class ExController {
@Autowired
private BookService bookService;
@GetMapping("/")
public String home() {
return "Welcome to the Book API!";
}
@GetMapping("/findbyid/{id}")
public Book findBookById(@PathVariable int id) {
return bookService.findBookById(id);
}
@GetMapping("/findall")
public List<Book> findAllBooks() {
return bookService.findAllBooks();
}
@DeleteMapping("/delete")
public String deleteAllBooks() {
bookService.deleteAllBooks();
return "All books have been deleted.";
}
}
Step 6: Run the Application
The main application class, BookApiApplication, is automatically generated by Spring Boot. It contains the main method to run the application.
BookApiApplication.java:
Java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BookApiApplication {
public static void main(String[] args) {
SpringApplication.run(BookApiApplication.class, args);
}
}
Step 7: Test the APIs
API's list
- http://localhost:8080/->To save the data
- http://localhost:8080/findbyid/2-> Find Book by id
- http://localhost:8080/findall-> Find all books
- http://localhost:8080/delete-> Delete all books
Test the APIs using Postman to verify they work as expected
Explore
Spring Boot Tutorial
4 min read
Spring Boot Basics and Prerequisites
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP