Spring Boot makes it easy to create RESTful web services in Java with minimal configuration. REST APIs allow clients to interact with the server over HTTP using standard methods like GET, POST, PUT, and DELETE.
- Spring Boot simplifies API development with auto-configuration and embedded servers.
- REST APIs in Spring Boot typically exchange data in JSON format, making them lightweight and easy to integrate with front-end or mobile apps.
Step-by-Step Guide to Build a RESTful API using SpringBoot
Step 1. Create the Spring Boot project
Using STS (Spring Tool Suite / Eclipse):
- Go to File -> New-> Spring Starter Project
- Click Next
- Enter the following details: Project Name: book-api, Group: com.example, Artifact: book-api, Java Version: 17 and Build Tool: Maven
- Add Dependencies: Spring Web and Spring Boot DevTools
- Click Finish
Step 2: Project Structure
Once the project is created, we will see the following structure

Step 3: pom.xml Configuration
<?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 4: Create the POJO Class
A POJO (Plain Old Java Object) represents the data model.
Book.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 5: 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.Â
BookService Interface
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();
}
BookServiceImpl Class
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 6: Create the REST Controller
The Controller layer exposes the APIs to the client. Create a RestController class to handle HTTP requests.
ExController.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 7: 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:
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 8: Test the APIs
Available APIs
Endpoint | Description |
|---|---|
http://localhost:8080/api/ | Welcome message |
http://localhost:8080/api/findbyid/2 | Get book by ID |
http://localhost:8080/api/findall | Get all books |
http://localhost:8080/api/delete | Delete all books |
Test the APIs using Postman to verify they work as expected