Introduction: REST API in Java — Easier Than It Seems
Developing a REST API is one of the most in-demand skills in modern backend development. Java, thanks to its reliability and vast ecosystem, remains a leader for building industrial APIs. Many beginners think writing a REST API in Java is difficult and time-consuming. In reality, with the right tool (Spring Boot), you can create a working service in 30 minutes.
In this article, we will practically break down how to quickly create a full-fledged REST API in Java. We will write a small application for managing a list of books, which supports standard CRUD operations: creating, reading, updating, and deleting records. All the code will be ready to run, and you can test it via Postman or a browser.
1. Project Setup and Spring Boot Configuration
The fastest way to create a project is to use Spring Initializr. This is the official generator that creates a project skeleton with the necessary dependencies.
1.1. Project Generation
Go to start.spring.io and select:
- Project: Maven (or Gradle)
- Language: Java
- Spring Boot: latest stable version (e.g., 3.2.x)
- Group: com.example
- Artifact: bookapi
Add dependencies:
Spring Web— for creating REST controllersSpring Data JPA— for working with the databaseH2 Database— embedded database for testing
Click "Generate", download the archive, and unzip it. Open the project in IntelliJ IDEA or any other IDE.
1.2. Project Structure
After opening the project, you will see the following structure:
bookapi/├── src/│ ├── main/│ │ ├── java/com/example/bookapi/│ │ │ ├── BookapiApplication.java│ │ │ ├── controller/│ │ │ ├── model/│ │ │ └── repository/│ │ └── resources/│ │ └── application.properties│ └── test/└── pom.xmlCreate the controller, model, and repository packages manually — this will make navigation easier.
2. Creating the Data Model and Repository
Let's start with the model — a class that describes the "Book" entity.
2.1. Book Class (Model)
Create the file Book.java in the model package:
package com.example.bookapi.model;
import jakarta.persistence.*;
@Entity@Table(name = "books")public class Book {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@Column(nullable = false) private String title;
@Column(nullable = false) private String author;
private int year;
public Book() {}
public Book(String title, String author, int year) { this.title = title; this.author = author; this.year = year; }
// 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 String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; }
public int getYear() { return year; } public void setYear(int year) { this.year = year; }}2.2. Repository
Create the interface BookRepository.java in the repository package:
package com.example.bookapi.repository;
import com.example.bookapi.model.Book;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.stereotype.Repository;
import java.util.L