How to Write a REST API in Java in 30 Minutes: For Beginners
Creating a REST API is one of the key skills of a modern developer. If you are just starting to learn Java and want to quickly create your first web service, this guide is for you. We will go from installing tools to a working API that can be tested in a browser or Postman. The entire process will take no more than 30 minutes.
In this article, we will use Spring Boot — the most popular framework for creating web applications in Java. It handles all the routine setup, allowing you to focus on the logic. As a result, you will get a simple API for managing a to-do list (Todo List).
What is a REST API and Why Do You Need It?
REST (Representational State Transfer) is an architectural style for building web services. A REST API works over the HTTP protocol and uses standard methods:
GET— retrieving dataPOST— creating a new recordPUT— updating an existing recordDELETE— deleting a record
Data is usually transmitted in JSON format. REST APIs are used everywhere: from mobile applications to microservice architecture. Mastering it will allow you to create a backend for any client.
Step 1: Environment Setup
We will need three things to get started. Make sure they are installed on your computer.
1.1. JDK (Java Development Kit)
Download and install JDK 17 or newer from the official Oracle website or use OpenJDK. Verify the installation with the command in the terminal:
java -version1.2. IntelliJ IDEA (Community Edition)
This is a free development environment, ideally suited for Java. Download it from the official website.
1.3. Postman (optional)
A tool for testing APIs. You can download it from postman.com. For the first steps, you can get by with a browser.
Step 2: Creating a Spring Boot Project
The fastest way is to use Spring Initializr. This is a web tool that generates a project skeleton.
- Go to start.spring.io.
- Select: Project: Maven, Language: Java, Spring Boot: 3.x.x.
- In the Group field, enter
com.example, in Artifact —todoapi. - Click Add Dependencies and add Spring Web.
- Click Generate — a ZIP archive with the project will be downloaded.
- Unzip the archive and open the folder in IntelliJ IDEA.
The project structure will look like this:
src/main/java/com/example/todoapi/— main codesrc/main/resources/application.properties— settingspom.xml— Maven build file
Step 3: Creating the Data Model and Controller
Now we will write the code. First, let's create a simple model — the TodoItem class.
3.1. Model Class
Create a new Java class in the package com.example.todoapi with the name TodoItem.java:
package com.example.todoapi;
public class TodoItem { private Long id; private String title; private boolean completed;
// Default constructor public TodoItem() {}
// Parameterized constructor public TodoItem(Long id, String title, boolean completed) { this.id = id; this.title = title; this.completed = completed; }
// Getters and setters public L