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:
CREATE TABLE task (
id BIGINT NOT NULL,
description VARCHAR(200) NOT NULL,
priority BIGINT,
PRIMARY KEY (id)
);
Requirements
REST Endpoints
Create Task
Method: POST /tasks
Request Body:
{
"description": "Buy groceries",
"priority": 2
}
Responses:
201 Created: Task is successfully created.
400 Bad Request: If the description is missing or null.
{
"message": "Task description is required",
"status": 400
}
Get Task by ID
Method: GET /tasks/{id}
Responses:
200 OK: Returns the task object.
404 Not Found: If task does not exist.
{
"message": "Cannot find task with given id",
"status": 404
}