Simple Java SpringBoot CRUD API

Isaac Tonyloi
2 min readJan 18, 2024
Source: geeks for geeks

CRUD (Create, Read, Update, Delete) operations form the backbone of database interactions in many applications. In this example, we’ll explore how to implement CRUD operations using Java Spring Boot with a different API and delve into additional details.

Setting Up the Development Environment:

Start by installing essential software, such as the Java Development Kit (JDK) and a Java web server like Apache Tomcat.

Designing the Database Schema:

Define the necessary tables and fields for storing data. Tools like MySQL Workbench can aid in creating the database schema.

Creating the Model:

Develop Java classes representing data entities. For this example, let’s consider a Task entity for managing tasks.

public class Task {  

private Long id;

private String title;

private String description;

private boolean completed
// Additional fields and getters/setters go here

public Task() {}
public Task(Long id, String title, String description, boolean completed) {
this.id = id;
this.title = title;
this.description = description;
this.completed = completed;
}

// Additional methods for accessing and manipulating the data go here
}

Creating the Controller:

Use Spring controllers to handle HTTP requests. In this case, let’s create a TaskController.

@RestController
@RequestMapping("/tasks")
public class TaskController {

private final TaskService taskService;

public TaskController(TaskService taskService) {
this.taskService = taskService;
}
@GetMapping
public List<Task> getAllTasks() {
return taskService.getAllTasks();
}
@GetMapping("/{id}")
public Task getTask(@PathVariable Long id) {
return taskService.getTask(id);
}
@PostMapping
public Task createTask(@RequestBody Task task) {
return taskService.createTask(task);
}
@PutMapping("/{id}")
public Task updateTask(@PathVariable Long id, @RequestBody Task task) {
return taskService.updateTask(id, task);
}
@DeleteMapping("/{id}")
public void deleteTask(@PathVariable Long id) {
taskService.deleteTask(id);
}

Implementing the Service:

The service layer performs business logic. Let’s create a TaskService class.

@Service
public class TaskService {

private final TaskRepository taskRepository;

public TaskService(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
}

public List<Task> getAllTasks() {
return taskRepository.findAll();
}
public Task getTask(Long id) {
return taskRepository.findById(id).orElse(null);
}
public Task createTask(Task task) {
return taskRepository.save(task);
}
public Task updateTask(Long id, Task task) {
task.setId(id);
return taskRepository.save(task);
}
public void deleteTask(Long id) {
taskRepository.deleteById(id);
}
}

Creating the Repository:

The repository interfaces with the database. For this example, let’s use JPA with a TaskRepository.

@Repository
public interface TaskRepository extends JpaRepository<Task, Long> {
// Additional methods go here if needed
}

HTML Template for Displaying Task List:

If you require an HTML template, here’s a basic Thymeleaf template.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Task List</title>
</head>
<body>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Completed</th>
</tr>
<tr th:each="task : ${tasks}">
<td th:text="${task.id}"></td>
<td th:text="${task.title}"></td>
<td th:text="${task.description}"></td>
<td th:text="${task.completed}"></td>
</tr>
</table>
</body>
</html>

Configuration Properties:

For a MySQL database, the application.properties file could look like this:

spring.datasource.url=jdbc:mysql://localhost:3306/task_db
spring.datasource.username=root
spring.datasource.password=mypassword
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.thymeleaf.cache=false

That is it ! Thank you for reading !

--

--

Isaac Tonyloi

Software Engineer. Fascinated by Tech and Productivity. Writing mostly for myself, sharing some of it with you