Build a Simple REST API with Java 21 and Spring Boot

πŸš€ 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

Simple REST API with Java 21 and Spring Boot

πŸ—οΈ 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 record class 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 books
  • GET /api/books/{id} β€” Get a book by ID
  • POST /api/books β€” Create a new book
  • PUT /api/books/{id} β€” Update book
  • DELETE /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