Introduction: REST API is Easier Than It Seems
\nMany beginner developers think that writing a REST API in Java is a complex task requiring deep knowledge and long hours of work. In reality, with modern tools like Spring Boot, you can create a full-fledged RESTful service in literally 30 minutes. In this article, we will practically break down how to write a REST API in Java using a minimal set of tools. You will get a ready-made template that you can adapt for your projects.
\nREST (Representational State Transfer) is an architectural style that allows clients and servers to exchange data via HTTP. We will create a simple API for managing a to-do list. By the end of the article, you will have a working server that can create, read, update, and delete tasks (CRUD).
\n\nStep 1: Setting Up the Environment and Creating the Project
\nTo start, we will need:
\n- \n
- Java Development Kit (JDK) version 17 or higher. \n
- IntelliJ IDEA (Community Edition) or any other IDE. \n
- Maven for dependency management. \n
- Postman or a browser for testing. \n
Let's create a project using Spring Initializr (start.spring.io). Select the following settings:
\n- \n
- Project: Maven \n
- Language: Java \n
- Spring Boot: 3.2.x \n
- Dependencies: Spring Web, Spring Data JPA, H2 Database \n
Download the ZIP archive and unzip it. Open the project in your IDE. The structure will look like this:
\nsrc/\n └── main/\n ├── java/com/example/restapi/\n │ ├── RestapiApplication.java\n │ ├── controller/\n │ ├── model/\n │ └── repository/\n └── resources/\n └── application.properties\nThis is the base we will start from. The setup took literally 2 minutes.
\n\nStep 2: Creating the Data Model and Repository
\nLet's start with the model. Create a class Task.java in the model folder. This will be the entity representing a task:
package com.example.restapi.model;\n\nimport jakarta.persistence.*;\n\n@Entity\n@Table(name = "tasks")\npublic class Task {\n @Id\n @GeneratedValue(strategy = GenerationType.IDENTITY)\n private Long id;\n\n @Column(nullable = false)\n private String title;\n\n private boolean completed;\n\n // Constructors\n public Task() {}\n\n public Task(String title, boolean completed) {\n this.title = title;\n this.completed = completed;\n }\n\n // Getters and Setters\n public Long getId() { return id; }\n public void setId(Long id) { this.id = id; }\n public String getTitle() { return title; }\n public void setTitle(String title) { this.title = title; }\n public boolean isCompleted() { return completed; }\n public void setCompleted(boolean completed) { this.completed = completed; }\n}\nNow let's create the repository. In the repository folder, create an interface TaskRepository.java:
package com.example.restapi.repository;\n\nimport com.example.restapi.model.Task;\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.stereotype.Repository;\n\n@Repository\npublic interface TaskRepository extends JpaRepository<Task, Long> {\n // JpaRepository already provides all basic CRUD methods\n}\nAt this stage, we have described the data structure and the way to interact with the database. Spring Data JPA will automatically create the table in H2 and provide methods like findAll(), save(), deleteById(), and others. This saves hours of manual coding.
Step 3: Creating the Controller with REST Endpoints
\nNow for the most interesting part — the controller. In the controller folder, create a class TaskController.java. It will handle HTTP requests:
Blogs
Book Recommendations
Read also
Autoincrement Primary Key in Room: Magic and Performance
How to Convert Vec<char> to String in Rust: A Complete Guide
HTTP Client in JavaScript: Making Requests Properly in Node.js
Caching in TypeScript: Redis, Memcached, and Built-in Solutions — A Complete Guide
Deploying a Kotlin Application on VPS: A Step-by-Step Guide from A to Z