Featured Article

Spring Boot CRUD Operations Tutorial: Build REST APIs in Minutes

Kenneth Jul 13, 2026

Spring Boot, with its comprehensive feature set and ease of use, has gained significant traction in the Java development community. In many applications, performing CRUD (Create, Read, Update, Delete) operations is a fundamental requirement. Thus, a comprehensive understanding of these operations in Spring Boot is crucial for any developer. Let's delve into a step-by-step tutorial to master CRUD operations using Spring Boot.

Spring Boot Tutorial for Beginners 9 - Spring Boot CRUD Operations using Apache Derby
Spring Boot Tutorial for Beginners 9 - Spring Boot CRUD Operations using Apache Derby

Before we proceed, ensure you have Java Development Kit (JDK) version 8 or higher, Apache Maven, and Spring Boot installed on your system. Also, have your favorite code editor or IDE ready, such as IntelliJ IDEA or Eclipse.

Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets
Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets

Setting Up the Project and Dependency

Let's first create a new Spring Boot project using Spring Initializr (https://start.spring.io/). Choose 'Web' and 'Spring Data JPA' dependencies for this project. After generating the project, import it into your IDE.

Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets
Master Exception Handling in Spring Boot with ControllerAdvice | Unlock Spring Boot CRUD Secrets

This project setup includes Spring Boot, Spring Web, Spring Data JPA, and H2, a lightweight in-memory database, which are essential for performing CRUD operations.

Creating a Simple Entity Class

Spring Boot Tutorial 22 - CRUD Operations with JPA Repository
Spring Boot Tutorial 22 - CRUD Operations with JPA Repository

Create a new class 'User' in the 'com.example.demo' package with the annotations @Entity and @Table.

User Entity Definition

Here's a simple 'User' entity definition:

CRUD with React Js and Spring Boot
CRUD with React Js and Spring Boot

```java import javax.persistence.*; import java.util.Objects; @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Getters and setters //... } ```

Spring Boot will automatically create a table 'users' in the H2 database with 'id', 'name', and 'email' columns.

Creating a Repository Interface

A Repository interface extends JpaRepository and provides basic CRUD operations.

Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed

Here's a simple 'UserRepository' interface:

```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserRepository extends JpaRepository { List findByName(String name); User findByEmail(String email); } ```

The 'UserRepository' extends 'JpaRepository' and provides basic CRUD methods like 'save', 'findAll', 'findById', and 'delete'. Also, custom queries 'findByName' and 'findByEmail' are defined.

Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Complete spring boot mastery: Hands on development Bootcamp
Complete spring boot mastery: Hands on development Bootcamp
Advanced Spring Tutorial | Spring MVC, Spring Data, JDBC & CRUD Operations
Advanced Spring Tutorial | Spring MVC, Spring Data, JDBC & CRUD Operations
Spring Boot RESTful CRUD Example with MySQL Database - Websparrow
Spring Boot RESTful CRUD Example with MySQL Database - Websparrow
Spring Boot + Kotlin + AngularJS + WebJar CRUD Example Part 2 » Tell Me How - A Place for Technology Geekier
Spring Boot + Kotlin + AngularJS + WebJar CRUD Example Part 2 » Tell Me How - A Place for Technology Geekier
Mastering 'Get Users' RESTful Endpoint with Pagination in Spring Boot | RESTful Web Services
Mastering 'Get Users' RESTful Endpoint with Pagination in Spring Boot | RESTful Web Services
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
Spring Boot Tutorial for Beginners 7 - Spring Data JPA
Spring Boot Tutorial for Beginners 7 - Spring Data JPA

Creating a Controller Class

A Controller class handles HTTP requests and responses using annotations like @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping.

User Controller Definition

Here's a simple 'UserController' class:

```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public List getAllUsers() { return userRepository.findAll(); } @PostMapping public User createUser(@RequestBody User user) { return userRepository.save(user); } // More methods for update, delete, get by name, and get by email //... } ```

The 'UserController' has methods to handle HTTP GET, POST, PUT, and DELETE requests for performing CRUD operations on 'User' entities.

Performing CRUD Operations

Now, let's perform the actual CRUD operations using a tool like Postman or curl.

Create a New User

Send a POST request to 'http://localhost:8080/api/users' with the JSON body:

```json { "name": "John Doe", "email": "johndoe@email.com" } ```

You should receive a response with the newly created user's details, including the generated 'id'.

Retrieve All Users

Send a GET request to 'http://localhost:8080/api/users'. You should receive a response with a list of all the users in the system.

Update a User

Send a PUT request to 'http://localhost:8080/api/users/{id}' with the JSON body:

```json { "name": "Jane Doe", "email": "janedoe@email.com" } ```

You should receive a response with the updated user's details.

Delete a User

Send a DELETE request to 'http://localhost:8080/api/users/{id}'. You should receive a response with a success message.

Conclusion and Further Learning

In this tutorial, we demonstrated how to perform CRUD operations using Spring Boot, Spring Data JPA, and H2 in-memory database. With just a few annotations and interfaces, we created a simple yet functional REST API for managing user entities. This knowledge forms a solid foundation for building more complex applications with Spring Boot.

Now that you have grasped the basics, explore advanced concepts like exception handling, validation, and security. You can also look into Spring Data JPA's supporting methods, such as findByExample, queryByExample, and deleteByExample, to further enhance your CRUD operations. Happy coding!