Spring Boot Tutorial: Build Your First REST API in 15 Minutes

Spring Boot has revolutionized Java development by eliminating the “boilerplate” code that used to make Spring applications difficult to set up. Today, it is the industry standard for building robust, production-ready microservices.

In this tutorial, we will walk through the core concepts and steps to get your first RESTful web service up and running.


What is Spring Boot?

Spring Boot is an extension of the Spring framework that aims to simplify the development process. Its “convention over configuration” approach means it comes pre-configured with sensible defaults, allowing you to focus on writing business logic rather than XML configurations.

Prerequisites

Before we start, ensure you have the following installed:

  • Java Development Kit (JDK): Version 17 or 21 (LTS versions are recommended).

  • IDE: IntelliJ IDEA, Eclipse, or VS Code.

  • Maven: For dependency management (usually bundled with your IDE).


Step 1: Bootstrap Your Project

The easiest way to start is using the Spring Initializr (start.spring.io).

  1. Project: Maven Project

  2. Language: Java

  3. Spring Boot Version: 3.x.x (latest stable)

  4. Dependencies: Search for and add “Spring Web”.

  5. Click Generate to download the ZIP file and open it in your IDE.

Step 2: Understand the Project Structure

Once imported, you will see a standard Maven structure:

  • src/main/java: Where your Java code lives.

  • src/main/resources: Contains application.properties for configurations.

  • pom.xml: Your Maven configuration file containing the Spring Boot Starter Parent.

Step 3: Create Your First Domain Model

Let’s build a simple “Book” API. Create a new Java class named Book.java:

Java

public record Book(long id, String title, String author) {}

(Note: We are using Java Records for a concise data model.)

Step 4: Create the REST Controller

The Controller handles incoming HTTP requests and returns responses. Create a class named BookController.java and annotate it with @RestController.

  • @RestController: Tells Spring this class handles web requests.

  • @GetMapping: Maps HTTP GET requests to specific methods.

Java

@RestController
public class BookController {

    @GetMapping("/books")
    public List<Book> getBooks() {
        return List.of(
            new Book(1, "Spring Boot in Action", "Craig Walls"),
            new Book(2, "Clean Code", "Robert C. Martin")
        );
    }
}

Step 5: Run the Application

Navigate to your main application class (the one with the @SpringBootApplication annotation) and run the main method.

By default, Spring Boot starts an embedded Tomcat server on port 8080.

Step 6: Test Your API

Open your browser or a tool like Postman and navigate to: http://localhost:8080/books

You should see a JSON response containing your list of books!


Key Takeaways

  • Auto-configuration: Spring Boot automatically configures your app based on the dependencies in your pom.xml.

  • Embedded Server: No need to install a separate web server; it’s built-in.

  • Starters: The spring-boot-starter-web dependency includes everything you need for REST APIs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top