π Introduction
Spring Boot makes it easy to create production-ready REST APIs with minimal setup. In this guide, weβll walk you through building a simple REST API with Java 21 and Spring Boot using:
- Java 21 (LTS)
- Spring Boot 3+
- Maven
- IntelliJ IDEA or any IDE of your choice
Our API will manage a list of books with basic CRUD operations.
π Project Structure

ποΈ Step-by-Step Implementation
1. Create the Spring Boot Project
Use Spring Initializr with the following:
- Project: Maven
- Language: Java
- Spring Boot: 3.2+
- Dependencies: Spring Web, Spring Boot DevTools
2. pom.xml
4.0.0
com.kscodes
book-api
1.0.0
Book API
21
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-maven-plugin
3. Main Class β BookApiApplication.java
package com.kscodes.bookapi;
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);
}
}
4. Model β Book.java
package com.kscodes.bookapi.model;
public record Book(Long id, String title, String author) {}
Weβre using Java 21βs
recordclass to keep the model concise and immutable.
5. Service β BookService.java
package com.kscodes.bookapi.service;
import com.kscodes.bookapi.model.Book;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class BookService {
private final Map books = new HashMap<>();
private long currentId = 1;
public List getAllBooks() {
return new ArrayList<>(books.values());
}
public Book getBookById(Long id) {
return books.get(id);
}
public Book addBook(Book book) {
Book newBook = new Book(currentId++, book.title(), book.author());
books.put(newBook.id(), newBook);
return newBook;
}
public Book updateBook(Long id, Book updatedBook) {
if (!books.containsKey(id)) return null;
Book newBook = new Book(id, updatedBook.title(), updatedBook.author());
books.put(id, newBook);
return newBook;
}
public boolean deleteBook(Long id) {
return books.remove(id) != null;
}
}
6. Controller β BookController.java
package com.kscodes.bookapi.controller;
import com.kscodes.bookapi.model.Book;
import com.kscodes.bookapi.service.BookService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/books")
public class BookController {
private final BookService service;
public BookController(BookService service) {
this.service = service;
}
@GetMapping
public List getAllBooks() {
return service.getAllBooks();
}
@GetMapping("/{id}")
public Book getBook(@PathVariable Long id) {
return service.getBookById(id);
}
@PostMapping
public Book createBook(@RequestBody Book book) {
return service.addBook(book);
}
@PutMapping("/{id}")
public Book updateBook(@PathVariable Long id, @RequestBody Book book) {
return service.updateBook(id, book);
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable Long id) {
service.deleteBook(id);
}
}
7. application.properties
server.port=8080
π§ͺ Test Your API
Use Postman or curl to interact with the following endpoints:
GET /api/booksβ List all booksGET /api/books/{id}β Get a book by IDPOST /api/booksβ Create a new bookPUT /api/books/{id}β Update bookDELETE /api/books/{id}β Delete book
β Summary
Youβve just built a simple yet functional REST API with Java 21 and Spring Boot. We used record, REST annotations, and an in-memory service to manage book data.
You can download the complete source code here