A simple Spring Boot REST API for managing tasks, demonstrating:
- RESTful API design
- DTO pattern
- Service layer abstraction
- Exception handling
- Validation with
jakarta.validation
- Create a new task
- Update an existing task
- Retrieve a task by ID
- Retrieve all tasks
- JSON-based API responses with status codes and messages
- Java 17+
- Spring Boot 3+
- Spring Web
- Spring Validation
- Lombok
- Maven
src/
├─ main/java/com/example/simple_spring_api/
│ ├─ controller/ # REST Controllers
│ ├─ dto/ # Data Transfer Objects
│ ├─ exceptions/ # Custom Exceptions
│ ├─ model/ # Entity Classes
│ ├─ service/ # Service Layer
│ └─ SimpleSpringApiApplication.java
└─ resources/
└─ application.properties
git clone https://github.com/ashish-panicker/simple-spring-api.git
cd simple-spring-apimvn clean installmvn spring-boot:runThe application will start at:
http://localhost:8080
POST /api/v1/tasks
Content-Type: application/json
Request Body:
{
"title": "Learn Spring Boot",
"description": "Study REST API development"
}Response:
{
"status": "CREATED",
"message": "Task created successfully",
"data": {
"id": 1,
"name": "Learn Spring Boot",
"description": "Study REST API development",
"completed": false
}
}PUT /api/v1/tasks
Content-Type: application/json
Request Body:
{
"id": 1,
"title": "Learn Spring Boot - Updated",
"description": "Study REST API development in depth"
}Response:
{
"status": "OK",
"message": "Task updated successfully",
"data": {
"id": 1,
"name": "Learn Spring Boot - Updated",
"description": "Study REST API development in depth",
"completed": false
}
}GET /api/v1/tasks/{taskId}
Response:
{
"status": "OK",
"message": "Task fetched successfully",
"data": {
"id": 1,
"name": "Learn Spring Boot",
"description": "Study REST API development",
"completed": false
}
}GET /api/v1/tasks
Response (when tasks exist):
{
"status": "OK",
"message": "Task fetched successfully",
"data": [
{
"id": 1,
"name": "Learn Spring Boot",
"description": "Study REST API development",
"completed": false
}
]
}Response (when no tasks exist):
{
"status": "NO_CONTENT",
"message": "No tasks found",
"data": []
}- Request DTOs are validated with
@Valid— invalid data will return400 Bad Request. - Custom
TaskNotFoundExceptionis thrown if a task is not found. - Uses
ResponseDtofor consistent API responses.
This project is licensed under the MIT License.