DEV Community

Cover image for Building Your First REST API with Spring Boot
Deividas Strole
Deividas Strole

Posted on

Building Your First REST API with Spring Boot

By Deividas Strole — Full-Stack Developer

Introduction

If you're a developer, you definitely know Spring Boot. Just last year we celebrated the 10-year anniversary of this super Java framework. And like the Java programming language itself, Spring Boot doesn't show its age and keeps going strong. It's still the most popular framework for building production REST APIs in Java.

In this tutorial, I'll walk you through building a simple REST API with Spring Boot for managing books—and you'll learn along the way. Great for beginners and those who already forgot! :)

What We'll Build
A Book API where you can:

  • Add a book
  • Get all books
  • Get a book by ID
  • Delete a book

Step 1: Create Project
Go to start.spring.io and select:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.0 or latest
  • Dependencies: Spring Web

Click "Generate" and unzip the project.

Step 2: Create Book Class
File: src/main/java/com/example/demo/Book.java

javapackage com.example.demo;

public class Book {
    private Long id;
    private String title;
    private String author;

    // Constructor
    public Book(Long id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long 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;
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Create Controller
File: src/main/java/com/example/demo/BookController.java

javapackage com.example.demo;

import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/books")
public class BookController {

    private List<Book> books = new ArrayList<>();
    private Long nextId = 1L;

    // Get all books
    @GetMapping
    public List<Book> getAllBooks() {
        return books;
    }

    // Get book by ID
    @GetMapping("/{id}")
    public Book getBookById(@PathVariable Long id) {
        return books.stream()
                .filter(book -> book.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    // Add a new book
    @PostMapping
    public Book addBook(@RequestBody Book book) {
        book.setId(nextId++);
        books.add(book);
        return book;
    }

    // Delete a book
    @DeleteMapping("/{id}")
    public String deleteBook(@PathVariable Long id) {
        books.removeIf(book -> book.getId().equals(id));
        return "Book deleted";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Run the Application
File: src/main/java/com/example/demo/DemoApplication.java (already exists)

Just run this file. Your API will start on http://localhost:8080

Step 5: Test Your API
Open a new terminal and try these commands:

Add a book:

bash
curl -X POST http://localhost:8080/api/books \
  -H "Content-Type: application/json" \
  -d '{"title":"Harry Potter","author":"J.K. Rowling"}'
Enter fullscreen mode Exit fullscreen mode

Get all books:

bash
curl http://localhost:8080/api/books
Enter fullscreen mode Exit fullscreen mode

Get book by ID:

bash
curl http://localhost:8080/api/books/1
Enter fullscreen mode Exit fullscreen mode

Delete a book:

bash
curl -X DELETE http://localhost:8080/api/books/1
Enter fullscreen mode Exit fullscreen mode

How It Works

  • @RestController - tells Spring this class handles web requests
  • @RequestMapping("/api/books") - all endpoints start with /api/books
  • @GetMapping - handles GET requests (retrieve data)
  • @PostMapping - handles POST requests (create data)
  • @DeleteMapping - handles DELETE requests (remove data)
  • @RequestBody - converts JSON to Java object
  • @PathVariable - gets the ID from URL

And, you are finished!!! You have a working REST API with just 2 files (well, maybe 3 including the main application file :) ).

About the Author

Deividas Strole is a Full-Stack Developer based in California and specializing in Java, Spring Boot, React, and AI-driven development.
He writes about software engineering and modern full-stack development.

🔗 Connect with me

YouTube: https://youtube.com/@deividas-strole

GitHub: https://github.com/deividas-strole

LinkedIn: https://linkedin.com/in/deividas-strole

Top comments (0)