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.RELEASE
Hibernate: 5.2.17.Final
Dependencies:
spring-boot-starter-data-jpa
spring-boot-starter-web
The database schema is pre-configured and running.
Table schema:
CREATETABLEtask ( id BIGINTNOT NULL,descriptionVARCHAR(200) NOT NULL,priority BIGINT,PRIMARY KEY (id));
Requirements
REST Endpoints
Create Task
Method: POST /tasks
Request 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 /tasks
Response:
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.