How to Write a REST API in Java in 30 Minutes
Modern web development is unthinkable without REST API — an architectural style that allows clients and servers to exchange data over the HTTP protocol. Java, thanks to its reliability and vast ecosystem, remains one of the best languages for creating backend services. In this article, we will show you how to write a full-fledged REST API in Java using Spring Boot — the most popular framework for microservices — in 30 minutes. You will create a simple CRUD service for managing a Todo List, which can be deployed and tested immediately.
What is REST API and Why Java?
REST (Representational State Transfer) is a set of principles where each resource (e.g., user, task, or product) has a unique URL, and interaction occurs through standard HTTP methods: GET (retrieval), POST (creation), PUT (update), and DELETE (deletion). Java with the Spring Boot framework provides ready-made annotations and tools to implement a REST API in literally minutes. You don't need to manually parse JSON or configure a server — Spring does all of that.
Step-by-Step Guide: Writing a REST API in Java in 30 Minutes
Step 1. Project Setup and Dependencies
The fastest way to start is to use Spring Initializr. Select the following parameters:
- Project: Maven
- Language: Java
- Spring Boot: 3.2.0 (or the latest stable version)
- Dependencies: Spring Web, Spring Data JPA, H2 Database
Click "Generate", download the ZIP archive, and unzip it. Open the project in your IDE (IntelliJ IDEA, Eclipse, or VS Code). The pom.xml file should contain the following dependencies:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency></dependencies>The H2 database is a built-in in-memory DBMS that requires no installation. For production, you would replace it with PostgreSQL or MySQL.
Step 2. Creating the Data Model (Entity)
Create a package model and inside it a class TodoItem.java. This will be the entity that maps to a table in the database.
package com.example.demo.model;
import jakarta.persistence.*;
@Entity@Table(name = "todos")public class TodoItem {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@Column(nullable = false) private String title;
private boolean completed;
// Constructors public TodoItem() {}
public TodoItem(String title, boolean completed) { this.title = title; this.completed = completed; }
// 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 boolean isCompleted() { return completed; } public void setCompleted(boolean completed) { this.completed = completed; }}Here we use JPA annotations: @Entity tells Spring that the class is a table, @Id is the primary key, and @GeneratedValue is for auto-generation of the ID.