Featured Article

Build a Spring Boot CRUD REST API Example Step by Step

Kenneth Jul 13, 2026

Spring Boot, a popular Java-based framework, simplifies the development of micro-service architectures. One of its standout features is the ease with which it allows you to create REST APIs. This article will guide you through creating a comprehensive CRUD (Create, Retrieve, Update, Delete) REST API using Spring Boot.

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

Before we jump into the code, ensure you have Spring Boot installed, along with your favorite Integrated Development Environment (IDE). For this example, we'll use Spring Boot's Spring Initializr tool to generate a basic project with Web and JPA dependencies.

Spring Restful Web Services JSON CRUD Example - Dinesh on Java
Spring Restful Web Services JSON CRUD Example - Dinesh on Java

Setting Up the Project

Once you've generated the project, you'll see an empty application. Let's set up a simple model class to work with. Create a new Java class named 'User'.

Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot Project Ideas for Resume | Real-World Examples

For simplicity, let's use the following User class with attributes like id, name, and email.

``` public class User { private Long id; private String name; private String email; // Getters and Setters } ```

Spring Data JPA Repository

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

Now, create a JPA repository for the User class. This will allow us to perform standard CRUD operations provided by Spring Data JPA.

Create a new interface named 'UserRepository' and extend it with 'JpaRepository'.

``` public interface UserRepository extends JpaRepository {} ```

Controller Class for REST API

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

Next, create a REST controller class to handle the HTTP request/response conversions. In this case, we'll name it 'UserController'.

Here's a basic example of how your controller might look:

``` @RestController @RequestMapping("/api/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public List getAllUsers() { return userRepository.findAll(); } @PostMapping public ResponseEntity createUser(@RequestBody User user) { User savedUser = userRepository.save(user); return new ResponseEntity<>(savedUser, HttpStatus.CREATED); } // Implement similar methods for update and delete operations } ```

That's it! You've now created a fully functional CRUD REST API. You can test these API calls using any online REST API testing tool or by creating a simple front-end application in JavaScript or any other language.

Spring Boot cheatsheet - Masterspringboot
Spring Boot cheatsheet - Masterspringboot

Implementing Exception Handling

Now that we have the basics covered, let's add some exception handling to our API. Spring Boot provides a great way to handle exceptions using the @ControllerAdvice and @ExceptionHandler annotations.

Spring Boot CRUD and JPA: Your Ultimate Book Example Guide
Spring Boot CRUD and JPA: Your Ultimate Book Example Guide
Building your first REST API: Java and Spring Boot
Building your first REST API: Java and Spring Boot
How to Create a REST API With Spring Boot
How to Create a REST API With Spring Boot
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Spring Boot MongoDB Java Tutorial With Examples - JavaTechOnline
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
Unlocking JSON in Restful Web Services: Spring Boot, JPA & Jersey Explained!
Top Spring Boot Annotations Every Developer Must Know
Top Spring Boot Annotations Every Developer Must Know
Login and Signup with Java and Spring Boot
Login and Signup with Java and Spring Boot
7 French Spring Boots
7 French Spring Boots
15 Upcycled Easter Front Porch Decor For A Fresh Look
15 Upcycled Easter Front Porch Decor For A Fresh Look

Global Exception Handler

Create a new class named 'GlobalExceptionHandler' with the @ControllerAdvice and @Order annotations. This will allow us to handle exceptions globally in a centralized manner.

Here's a simple example of how you can handle exceptions:

``` @ControllerAdvice @Order(Ordered.HIGHEST_PRECEDENCE) public class GlobalExceptionHandler { @ExceptionHandler(ConstraintViolationException.class) public ResponseEntity handleConstraintViolationException(ConstraintViolationException ex) { return new ResponseEntity<>("Validation error occurred", HttpStatus.BAD_REQUEST); } } ```

Custom Exception Handling

You can also create custom exceptions and handle them in a similar way.

Create a new class named 'ResourceNotFoundException' and throw it when a resource is not found:

``` public class ResourceNotFoundException extends Exception { public ResourceNotFoundException(String message) { super(message); } } ```

Then, handle this custom exception in the 'UserController' class:

``` @GetMapping("/{id}") public ResponseEntity getUserById(@PathVariable Long id) { return userRepository.findById(id) .map(user -> new ResponseEntity<>(user, HttpStatus.OK)) .orElseThrow(() -> new ResourceNotFoundException("User not found")); } ```

Your final REST API should now be robust with proper exception handling and CRUD operations. This way, you can build scalable and secure applications using Spring Boot.

Spring Boot's ability to simplify RESTful APIs makes it an ideal choice for modern, cloud-native applications. What's next? Explore Spring Security for adding authentication and authorization to your APIs, or look into Spring WebFlux for non-blocking API development.