🚀 Getting Started with Spring Boot: Build Your First REST API in 15 Minutes

Ranjana SinghRanjana Singh
3 min read

Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. If you're a Java developer looking to get started with backend development or REST APIs, this guide will walk you through creating your very first Spring Boot application in just 15 minutes.

We’ll build a simple Book Manager API that allows you to add and retrieve books.


🛠️ Prerequisites

Make sure you have the following installed:

  • Java 17 or above

  • Maven or Gradle

  • An IDE (IntelliJ IDEA, Eclipse, or VSCode)

  • Basic knowledge of Java


📦 Step 1: Initialize the Spring Boot Project

Go to Spring Initializr and generate a new project with the following settings:

  • Project: Maven

  • Language: Java

  • Spring Boot Version: (Choose the latest stable)

  • Dependencies:

    • Spring Web

Click "Generate", unzip the downloaded file, and open it in your favorite IDE.


📄 Step 2: Create the Book Model

Inside the src/main/java/... package, create a new class Book.java.

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

    // Constructors
    public Book() {}

    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; }
}

🎯 Step 3: Create the Book Controller

Now create a new class called BookController.java in the same package.

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

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

    private List<Book> bookList = new ArrayList<>();

    @GetMapping
    public List<Book> getAllBooks() {
        return bookList;
    }

    @PostMapping
    public Book addBook(@RequestBody Book book) {
        bookList.add(book);
        return book;
    }
}

▶️ Step 4: Run the Application

Locate the DemoApplication.java (or the main class) and run it using your IDE, or via terminal:

./mvnw spring-boot:run

Once running, your app will be available at http://localhost:8080.


📬 Test the API Endpoints

Use Postman, curl, or any REST client to test the API.

1. GET all books

GET http://localhost:8080/api/books

Returns: []

2. POST a new book

POST http://localhost:8080/api/books Content-Type: application/json

Request body:

{ "id": 1, "title": "Clean Code", "author": "Robert C. Martin" }

Response:

{ "id": 1, "title": "Clean Code", "author": "Robert C. Martin" }


🧠 What You Learned

  • How to create a Spring Boot project using Spring Initializr

  • How to build a simple REST API with @RestController

  • How to test GET and POST endpoints


🔜 What’s Next?

In the next blog, we’ll connect this API to a real database using Spring Data JPA and add full CRUD operations (Create, Read, Update, Delete).

Have questions or ideas for the next post? Drop a comment below!


Happy coding!

0
Subscribe to my newsletter

Read articles from Ranjana Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ranjana Singh
Ranjana Singh