How to Write a REST API in Java in 30 Minutes: A Step-by-Step Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Write a REST API in Java in 30 Minutes: A Step-by-Step Guide



Creating a REST API is one of the key skills of a modern Java developer. REST (Representational State Transfer) allows you to build lightweight, scalable, and understandable web services. In this guide, we will create a full-fledged REST API using Spring Boot in 30 minutes. You will learn how to set up a project, create endpoints for working with users, handle JSON, and test the API via Postman or a browser.



1. What is a REST API and why is it needed?



A REST API is an architectural style where the client and server exchange data via HTTP requests. The basic principles of REST are:



  • Resources — each object (user, product, order) is represented by a URL.
  • HTTP methods — GET (read), POST (create), PUT (update), DELETE (delete).
  • Stateless — each request contains all the necessary information; the server does not store session state.
  • Data formats — most often JSON or XML.


REST API is used in mobile applications, web frontends, microservices, and system integration. In Java, the most popular framework for creating REST APIs is Spring Boot — it provides ready-made annotations and automatic configuration.



2. Setting up a Spring Boot project



To get started, you will need:

  • JDK 17 or higher
  • IntelliJ IDEA or any other IDE
  • Maven or Gradle


Let's create a project using Spring Initializr. Select:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.0 (or the latest stable version)
  • Dependencies: Spring Web, Spring Boot DevTools


Click "Generate" and unzip the archive. Open the project in your IDE. The structure will look like this:



rest-api-demo/├── src/main/java/com/example/restapidemo/│   ├── RestApiDemoApplication.java│   └── controller/│       └── UserController.java├── src/main/resources/│   └── application.properties└── pom.xml


3. Creating the data model and controller



Let's create a simple User model (class in the model package):



package com.example.restapidemo.model;

public class User { private Long id; private String name; private String email;

public User() {}

public User(Long id, String name, String email) { this.id = id; this.name = name; this.email = email; }

// Getters and Setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }}


Now let's create the UserController in the controller package. This is the heart of our API:



package com.example.restapidemo.controller;

import com.example.restapidemo.model.User;import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;import java.util.List;import java.util.concurrent.atomic.AtomicLong;

@RestController@RequestMapping("/api/users")public class UserController {

private final List<User> users = new ArrayList<>(); private final AtomicLong counter = new AtomicLong();

public UserController() { // Test data users.add(new User(counter.incrementAndGet(), "Alice", "alice@example.com")); users.add(new User(counter.incrementAndGet(), "Bob", "bob@example.com")); }

// GET /api/users — get all users @GetMapping public List<

Blogs

Book Recommendations