Building Your First REST API in Spring Boot

Spring Boot offers a powerful and minimalistic way to build modern REST APIs in Java. Whether you’re just getting started with backend development or coming from a different stack, Spring Boot simplifies a lot of the boilerplate code.

In this guide, you’ll learn how to create First REST API in Spring Boot :

  • Set up a Spring Boot project
  • Create a simple RESTful controller
  • Handle basic CRUD operations (GET, POST, PUT, DELETE)
  • Test the API using Postman or curl

Let’s begin! πŸš€

Building Your First REST API in Spring Boot

βœ… What is a REST API?

REST (Representational State Transfer) is an architectural style that uses standard HTTP methods like:

  • GET β€” retrieve data
  • POST β€” create new data
  • PUT β€” update existing data
  • DELETE β€” remove data

Spring Boot makes it easy to implement these with annotations like @RestController, @GetMapping, @PostMapping, etc.

βš™οΈ Step 1: Project Setup Using Spring Initializr

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

  • Project: Maven
  • Language: Java
  • Spring Boot: Latest stable version
  • Group: com.kscodes.springboot
  • Artifact: firstapp
  • Dependencies:
    • Spring Web
    • Spring Boot DevTools (optional for auto-reload)
    • Lombok (optional for boilerplate reduction)

Click Generate, unzip the file, and open the project in your IDE (IntelliJ or Eclipse).

πŸ—‚οΈ Step 2: Project Structure

πŸ“¦ Step 3: Create the User Model

πŸ”§ Step 4: Create the User Controller

▢️ Step 5: Run the Application

Run the main class:

πŸ§ͺ Step 6: Test the API

You can test the API using Postman, curl, or any HTTP client.

πŸ”Ή Get All Users

πŸ”Ή Create a User

πŸ”Ή Get User by ID

πŸ”Ή Update a User

πŸ”Ή Delete a User

βœ… Summary

You’ve successfully created your First REST API in Spring Boot that:

  • Uses an in-memory map for storing users
  • Supports full CRUD operations
  • Can be extended to use databases or services in the future