Implementing a RESTful Web API with Spring Boot
Source: Dev.to
RESTful API?
REST (Representational State Transfer) is an architectural style that defines a set of rules for creating scalable web services. REST APIs use HTTP methods like:
- GET – Retrieve data
- POST – Create data
- PUT – Update data
- DELETE – Remove data
REST APIs are stateless, meaning each request is independent and contains all required information.
What We Are Going to Build?
We will build a Task Tracker REST API with Spring Boot. The goal is to design a clean, meaningful, and production‑ready API that allows users to manage their daily tasks efficiently.
Endpoints We Will Build (Use Cases)
| Endpoint | HTTP Method | Use Case |
|---|---|---|
POST /api/tasks | Create a Task | Add a new task with title, description, category, and default status PENDING |
GET /api/tasks | Get All Tasks | Retrieve all tasks available in the system |
GET /api/tasks/{id} | Get Task by ID | Fetch details of a single task using its ID |
PUT /api/tasks/{id} | Update a Task | Modify a task’s details such as title, description, category, or status |
DELETE /api/tasks/{id} | Delete a Task | Remove a task from the system |
GET /api/tasks/status/{status} | Get Tasks by Status | Filter tasks based on their status — PENDING, COMPLETED |
Database Used: H2 In‑Memory Database
For simplicity and ease of demonstration we use H2, which offers:
- Zero installation
- Automatic console UI
- Fast development
- Easy testing
Note: It resets on application restart, which is perfect for tutorial purposes.
Layered Architecture Overview
A clean and maintainable REST API follows a layered architecture consisting of three main layers:
- Controller – Handles incoming API requests.
- Service – Contains business logic.
- Repository – Manages database interactions.
Project Structure
Setting Up the Project
1. Create a New Spring Boot Project
Go to Spring Initializr and configure the project:
- Project: Maven
- Language: Java
- Spring Boot Version: Latest stable
- Group:
com.example(or your preferred package) - Artifact:
task-tracker-api - Name:
Task Tracker API - Packaging: Jar
- Java Version: 17 or above
2. Add the Required Dependencies
Select the following dependencies:
- Spring Web – Create REST controllers and expose HTTP endpoints.
- Spring Data JPA – ORM‑based database interaction.
- H2 Database – Lightweight in‑memory database for development/testing.
- Lombok – Reduces boilerplate code (getters, setters, constructors, etc.).

Generate the project, unzip it, and open it in your IDE (IntelliJ IDEA, Eclipse, VS Code, etc.).
3. Run the Application
Locate the main class TaskTrackerApiApplication.java and run it. You should see a log line similar to:
Started TaskTrackerApiApplication in X seconds
Your Spring Boot application is now running.
Controller Layer
Create a class TaskController in the controller package. This layer handles incoming HTTP requests and sends responses back to the client.
package com.arshadpatel.task_tracker_api.controller;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TaskController {
// 1. Create a Task
// 2. Get All Tasks
// 3. Get Task by ID
// 4. Update a Task
// 5. Delete a Task
// 6. Get Tasks by Status
}
Model
Create Task.java in the model package.
package com.arshadpatel.task_tracker_api.model;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String status;
}
Annotation meanings
@Data– Generates getters, setters,toString(),equals(), andhashCode().@NoArgsConstructor– No‑args constructor.@AllArgsConstructor– All‑args constructor.@Entity– Marks the class as a JPA entity (database table).@Id– Primary key.@GeneratedValue– Auto‑generates the primary key value.
Repository and Service Layers
Repository
package com.arshadpatel.task_tracker_api.repository;
import com.arshadpatel.task_tracker_api.model.Task;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TaskRepository extends JpaRepository<Task, Long> {
}
Service
package com.arshadpatel.task_tracker_api.service;
import com.arshadpatel.task_tracker_api.model.Task;
import com.arshadpatel.task_tracker_api.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TaskService {
@Autowired
private TaskRepository taskRepository;
// 1. Create a Task
// 2. Get All Tasks
// 3. Get Task by ID
// 4. Update Task
// 5. Delete Task
// 6. Get Tasks by Status
}
Connecting Controller and Service
Implement the first API method in the controller to create a task. After implementation, you can test it using Postman or any API client by sending a POST request with a JSON body.
package com.arshadpatel.task_tracker_api.controller;
import com.arshadpatel.task_tracker_api.model.Task;
import com.arshadpatel.task_tracker_api.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/tasks")
public class TaskController {
@Autowired
private TaskService taskService;
@PostMapping
public Task createTask(@RequestBody Task task) {
// Call service to save the task
return taskService.createTask(task);
}
// Additional endpoint implementations go here...
}
Continue implementing the remaining endpoints (GET, PUT, DELETE, etc.) following the same pattern, delegating the core logic to TaskService. Once all methods are in place, you have a fully functional Task Tracker REST API built with Spring Boot.
