JPA
Task Management API
Problem Statement
You are developing a simple Task Management API for a to-do list application. The API should allow users to Create, Read, Update, and Delete tasks stored in a relational database. Each task has a description and an optional priority level.
You are required to implement the backend API using Spring Boot, Spring Data JPA, and Hibernate, following RESTful principles. The application should return proper HTTP status codes and meaningful JSON error messages when something goes wrong. Incomplete code is given and task is to complete it.
Given
Java: JDK 8
Spring Boot:
2.0.5.RELEASEHibernate:
5.2.17.FinalDependencies:
spring-boot-starter-data-jpaspring-boot-starter-web
The database schema is pre-configured and running.
Table schema:
CREATE TABLE task (
id BIGINT NOT NULL,
description VARCHAR(200) NOT NULL,
priority BIGINT,
PRIMARY KEY (id)
);Requirements
REST Endpoints
Create Task
Method:
POST /tasksRequest Body:
Responses:
201 Created: Task is successfully created.400 Bad Request: If the description is missing or null.
Get Task by ID
Method:
GET /tasks/{id}Responses:
200 OK: Returns the task object.404 Not Found: If task does not exist.
Get All Tasks
Method:
GET /tasksResponse:
200 OK: Returns a list of all tasks.
Update Task
Method:
PUT /tasks/{id}Request Body:
Behavior:
Updates the task's description and priority.
Responses:
200 OK: Returns the updated task.400 Bad Request: If the description is missing or null.404 Not Found: If task does not exist.
Delete Task
Method:
DELETE /tasks/{id}Responses:
204 No Content: If task is successfully deleted.404 Not Found: If task does not exist.
Incomplete Code
The following code snippet has been provided:
Solution
Task.java – Hibernate Entity
Task.java – Hibernate EntityTaskRepository.java
TaskRepository.javaTaskController.java
TaskController.javaLast updated