Day 2 - Updated the REST API Project using ResponseEntity
Refactoring the Controller Response While building the API, I made a small improvement to the controller response handling. Initially, the controller returned the data directly. Later, I updated it...

Source: DEV Community
Refactoring the Controller Response While building the API, I made a small improvement to the controller response handling. Initially, the controller returned the data directly. Later, I updated it to use ResponseEntity to have better control over the HTTP response. Before @GetMapping public List<Task> getAllTasks() { return taskService.getAllTasks(); } What happens here Spring automatically returns 200 OK Response body contains the list of tasks No direct control over HTTP response structure After @GetMapping public ResponseEntity<List<Task>> getAllTasks() { List<Task> tasks = taskService.getAllTasks(); return ResponseEntity.ok(tasks); } What improved The method now returns a complete HTTP response The HTTP status code is explicitly defined Easier to handle different scenarios like errors or empty responses Why This Matters Using ResponseEntity makes the API more flexible and REST-friendly. For example, we can now easily return different responses: return Respo