Learning Spring by doing. In this post, I'll share the steps I took to create my first Spring Boot application. There's less theory in this guide (you can find that elsewhere); instead, I'll focus on the steps to take, how to do them, and the reasoning behind those steps.
In my previous blog post, I created a controller with one request-handling method. It simply returned the string "Hello, World!" when you accessed the endpoint localhost:8081/hello, as the request-handling method was mapped to that endpoint. In Java, everything is an object, so the next thing we need is a simple POJO (Plain Old Java Object) class to carry our data. This could be a class like Student, Car, Employee, etc. In this case, we created a Task POJO class.
Next, we will create more request-handling methods that perform some logic on that data and return the output. To manipulate the data and save the results (for future retrieval), you typically need a database. However, as a beginner, I decided to use a simple list of Tasks and return its results instead.
List<Task> tasks = new ArrayList<>();
CRUD Operations:
- Create (HTTP POST Request): -
For the Create operation, I annotated the request-handling method with the @PostMapping annotation to map it to an endpoint. In this method, we fetch the JSON data sent by the client from the HTTP request body and create a Task object using that data. We do this with the @RequestBody annotation, which allows us to receive a pre-made object directly from Spring Boot. We then add this object to the list of tasks we created.
- Read ( HTTP GET Request): -
For the Read operation, I annotated the request-handling method with the @GetMapping annotation to map it to an endpoint. In this method, we just return the list of tasks that we created and added some task objects.
- Update (HTTP PUT Request): -
For the Create operation, I annotated the request-handling method with the @PutMapping annotation to map it to an endpoint. In this method, we fetch the JSON data sent by the client from the HTTP request body and create a Task object using that data. We do this with the @RequestBody annotation, which allows us to receive a pre-made object directly from Spring Boot. We also fetch the ID of the object to be updated from the request URL path variable. We use the @PathVariable annotation to get that ID. Then we replace the details of the existing object with the new object provided by Spring Boot, which was made using data from the client PUT request that came in.
- Delete (HTTP DELETE Request): -
For the Delete operation, I annotated the request-handling method with the @DeleteMapping annotation to map it to an endpoint. In this method, we fetch the ID of the object to be deleted from the request URL path variable. We use the @PathVariable annotation to get that ID. Then we delete that object from the list.
Code snippet: -
package com.example.demo.controllers;
import com.example.demo.entity.Task;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/tasks")
public class TaskController {
List<Task> tasks = new ArrayList<>();
int id = 1;
@GetMapping("/getTasks")
public List<Task> getAllTasks(){
return tasks;
}
@PostMapping("/addTask")
public List<Task> addTask(@RequestBody Task task){
task.setId(id++);
task.setCreatedAt(LocalDateTime.now());
task.setCompleted(false);
tasks.add(task);
return tasks;
}
@DeleteMapping("/deleteTask/{id}")
public List<Task> deleteTask(@PathVariable int id){
tasks.removeIf(task -> task.getId() == id);
return tasks;
}
@PutMapping("/updateTask/{id}")
public List<Task> updateTask(@PathVariable int id, @RequestBody Task updatedTask){
for(int i =0; i<tasks.size();i++){
if(tasks.get(i).getId() == id){
tasks.get(i).setTitle(updatedTask.getTitle());
tasks.get((i)).setCompleted(updatedTask.getCompleted());
tasks.get(i).setDescription(updatedTask.getDescription());
}
}
return tasks;
}
}
Top comments (0)