Featured Article

Build a Spring Boot API CRUD Tutorial: Step-by-Step Guide

Kenneth Jul 13, 2026

Spring Boot, a popular framework for building Java-based applications, simplifies the development of enterprise-grade microservices with its convention-over-configuration philosophy. One of the most fundamental operations in any application is the creation, retrieval, update, and deletion (CRUD) of resources. In this tutorial, we'll guide you through creating a Spring Boot API with full CRUD functionality.

How To Develop REST CRUD API Using Spring Boot ? - JavaTechOnline
How To Develop REST CRUD API Using Spring Boot ? - JavaTechOnline

Before we dive in, ensure you have Java Development Kit (JDK) 1.8 or later, Apache Maven, and your favorite text editor or IDE (like IntelliJ IDEA or Eclipse) installed. We'll also use Spring Boot's official start project, generated using start.spring.io, with Web and JPA dependencies.

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

Setting Up the Project Structure

The first step is to set up your project structure. Once you've generated the Spring Boot project, you'll see a few files and folders:

Spring Boot CRUD and JPA: Your Ultimate Book Example Guide
Spring Boot CRUD and JPA: Your Ultimate Book Example Guide

.Spring Boot project structure comprises the following folders: 'src', 'target', 'src/main/resources', and 'src/main/java'. The 'src/main/java' folder houses your application's Java source code and is organized by packages. We'll create a new package named 'com.example.demo' for our CRUD API.

Creating the Entity

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

Let's create a simple 'User' entity with fields for 'id', 'name', and 'email'. Spring Data JPA will automatically map these fields to a relational database table using the @Entity and @Id annotations.

The 'User.java' code might look like this:

```java import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // Getters and setters } ```

Implementing the Repository

How To Develop REST API Using Spring Boot ?
How To Develop REST API Using Spring Boot ?

Next, we implement the 'UserRepository' interface extending JpaRepository, which provides basic CRUD functionality out-of-the-box.

The 'UserRepository.java' code is as follows:

```java import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository { } ```

Creating the REST Controller

an image of some logos on a computer screen
an image of some logos on a computer screen

Now, let's create a 'UserController' that handles HTTP requests and responses for CRUD operations on our 'User' entity.

The '@RestController' annotation tells Spring that this controller should return objects directly as the response body, simplifying our API responses.

Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial
Spring Boot, MySQL, JPA, Hibernate Restful CRUD API Tutorial
Tutorial of Building Java REST API using Spring Boot and MongoDB – Djamware
Tutorial of Building Java REST API using Spring Boot and MongoDB – Djamware
Spring Boot Tutorial for Beginners 7 - Spring Data JPA
Spring Boot Tutorial for Beginners 7 - Spring Data JPA
Spring Boot Security OAuth2 Example
Spring Boot Security OAuth2 Example
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
how to draw boots step by step
how to draw boots step by step
🎯 How to Learn Backend Development – Step by Step Backend = Servers… | Suraj Dubey | 64 comments
🎯 How to Learn Backend Development – Step by Step Backend = Servers… | Suraj Dubey | 64 comments
Building your first REST API: Java and Spring Boot
Building your first REST API: Java and Spring Boot
Doodle Boots
Doodle Boots

Implementing Basic CRUD Operations

The 'UserController.java' code implements the CRUD methods as follows:

```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); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { return userRepository.save(user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userRepository.deleteById(id); } } ```

The '@GetMapping', '@PostMapping', '@GetMapping("/{id}")', '@PutMapping("/{id}")', and '@DeleteMapping("/{id}")' annotations map HTTP methods to their corresponding CRUD operations. The 'userRepository' injected by Spring handles the database operations.

Testing the API

Let's test our API using tools like Postman or curl. First, start your Spring Boot application and navigate to 'http://localhost:8080/api/users'. You should see an empty list as we haven't created any users yet.

Now, send a POST request with a JSON body containing the following user details:

```json { "name": "John Doe", "email": "john.doe@example.com" } ```

You'll receive a JSON response with the saved user's details, including the generated 'id'. Congratulations! You've just created your first Spring Boot API with full CRUD functionality.

Keep exploring and building with Spring Boot – there's a vast ecosystem of tools and libraries waiting to be discovered. Happy coding!