Spring Boot Starter Pack: Build REST APIs in 15 Minutes

Amit GuptaAmit Gupta
4 min read

So you want to build a REST API in Java — fast, clean, and production-ready? Spring Boot has your back. Whether you're a backend developer or just starting out, this guide will walk you through creating a fully functional REST API using Spring Boot 3+ — all in under 15 minutes.

TL;DR:

✅ No XML
✅ No over-engineering
✅ Just code that works

Let’s go from zero to RESTful hero in one sitting. 👇


🚀 Why Spring Boot?

Spring Boot simplifies the setup of Spring applications. You get:

  • Embedded web server (Tomcat by default)

  • Auto-configuration magic ✨

  • Minimal boilerplate

  • Production-ready features (Actuator, logging, etc.)

  • Huge ecosystem (Spring Security, Data JPA, WebFlux, etc.)


🔧 Step 1: Set Up Your Spring Boot Project

Go to: https://start.spring.io

Choose:

  • Project: Maven

  • Language: Java

  • Spring Boot: 3.x

  • Dependencies:

    • Spring Web

    • Spring Boot DevTools (optional)

    • Spring Data JPA

    • H2 Database

Project Name:

springboot-starter-api

Click Generate, unzip the project, and open it in your IDE (IntelliJ, VS Code, Eclipse — your call).


📁 Project Structure (Out of the Box)

textCopyEditsrc
 └── main
     ├── java
     │    └── com.example.springbootstarterapi
     │         └── SpringbootStarterApiApplication.java
     └── resources
          ├── application.properties
          └── static/

🧱 Step 2: Create Your First Model (Entity)

Let’s create a basic Book entity.

javaCopyEditpackage com.example.springbootstarterapi.model;

import jakarta.persistence.*;

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;
    private String author;

    // Constructors
    public Book() {}

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    // Getters and setters
    // ...
}

🗃️ Step 3: Repository Layer

Now we’ll create a repository interface using Spring Data JPA.

javaCopyEditpackage com.example.springbootstarterapi.repository;

import com.example.springbootstarterapi.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Long> {
}

That's it. You now have full CRUD support with zero implementation code.


javaCopyEditpackage com.example.springbootstarterapi.service;

import com.example.springbootstarterapi.model.Book;
import com.example.springbootstarterapi.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepo;

    public List<Book> getAllBooks() {
        return bookRepo.findAll();
    }

    public Book saveBook(Book book) {
        return bookRepo.save(book);
    }

    public Book getBookById(Long id) {
        return bookRepo.findById(id).orElse(null);
    }

    public void deleteBook(Long id) {
        bookRepo.deleteById(id);
    }
}

🌐 Step 5: Create a REST Controller

Here’s where it all comes together.

javaCopyEditpackage com.example.springbootstarterapi.controller;

import com.example.springbootstarterapi.model.Book;
import com.example.springbootstarterapi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    private BookService bookService;

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

    @GetMapping("/{id}")
    public Book getBook(@PathVariable Long id) {
        return bookService.getBookById(id);
    }

    @PostMapping
    public Book createBook(@RequestBody Book book) {
        return bookService.saveBook(book);
    }

    @DeleteMapping("/{id}")
    public void deleteBook(@PathVariable Long id) {
        bookService.deleteBook(id);
    }
}

⚙️ Step 6: Configure H2 In-Memory Database

Add the following to application.properties:

propertiesCopyEditspring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update

Visit http://localhost:8080/h2-console to view your DB.


🚀 Step 7: Run and Test Your API

In your IDE, run the main class:

javaCopyEdit@SpringBootApplication
public class SpringbootStarterApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootStarterApiApplication.class, args);
    }
}

Now open Postman or curl and try your API:

bashCopyEditGET     http://localhost:8080/api/books
POST    http://localhost:8080/api/books
DELETE  http://localhost:8080/api/books/{id}

Sample POST JSON:

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

🧪 BONUS: Add a Unit Test for Controller

Let’s add a quick test using JUnit and MockMvc.

javaCopyEdit@WebMvcTest(BookController.class)
public class BookControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BookService bookService;

    @Test
    void getAllBooks_shouldReturnEmptyList() throws Exception {
        when(bookService.getAllBooks()).thenReturn(Collections.emptyList());

        mockMvc.perform(get("/api/books"))
                .andExpect(status().isOk())
                .andExpect(content().json("[]"));
    }
}

Run it and watch the green tick. ✅


💡 Tips to Keep Going

Now that you’ve got the basics down:

  • Add validation with @Valid and @NotNull

  • Use DTOs instead of exposing entities directly

  • Connect to a real database like MySQL/PostgreSQL

  • Add Swagger UI with springdoc-openapi

  • Secure your API with Spring Security


🔚 Final Thoughts

You just built a working REST API in under 15 minutes using Spring Boot. 🎉

With just a few files and clear structure, you’ve unlocked a production-grade backend foundation. And the best part? Spring Boot scales with you — from small APIs to microservices and enterprise apps.

If you’re building your backend team or starting a product from scratch, make sure to hire Java developers who can wield Spring Boot confidently. It’s not just about writing code — it’s about shipping scalable APIs fast and reliably.

0
Subscribe to my newsletter

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

Written by

Amit Gupta
Amit Gupta