Featured Article

Java Spring Boot CRUD Tutorial: Build Your First REST API in Minutes

Kenneth Jul 13, 2026

Welcome to our comprehensive guide on creating a CRUD (Create, Read, Update, Delete) application using the popular Spring Boot framework in Java. Spring Boot offers a robust platform for building modern web applications, and its simplicity makes it a favorite among Java developers. In this tutorial, we'll walk you through the process of creating a CRUD API using Spring Boot, Spring Data JPA, and a relational database.

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

By the end of this guide, you'll have a solid understanding of how to create, read, update, and delete records in a database using Spring Boot. We'll also explore best practices and common tips to help you along your Spring Boot journey.

Java Spring Boot
Java Spring Boot

Setting Up the Spring Boot Project

The first step in our CRUD adventure is to set up a new Spring Boot project. If you haven't already, install the Spring Tool Suite (STS) or IntelliJ IDEA, as both offer excellent support for Spring Boot projects.

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

Once you have your IDE set up, create a new Spring Initializr project. Select 'Web' and 'Spring Data JPA' as dependencies, and choose your preferred database (e.g., MySQL, PostgreSQL). Download and import the generated project into your IDE.

Creating the Database and Entity Models

Spring Boot + MySQL Database example (CRUD) in Java [Tutorial]
Spring Boot + MySQL Database example (CRUD) in Java [Tutorial]

spring boot crud tutorial_create your database schema first, and then create the corresponding Entity models in your Java project. For example, if you're creating a simple 'Book' entity, your schema should have 'id', 'title', 'author', and 'publisher' columns, and your Java model should look like this:

`public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String author;
    private String publisher;
    // getters and setters
}`

Configuring the Application Properties

Spring Boot MongoDB CRUD Example
Spring Boot MongoDB CRUD Example

Spring Boot allows you to externalize configuration properties using the 'application.properties' or 'application.yml' file. Configure your database connection settings in this file, like so:

`spring.datasource.url=jdbc:mysql://localhost:3306/library_db
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update`

Setting 'ddl-auto' to 'update' will ensure that Hibernate creates the necessary tables in your database based on your Entity models.

Implementing the CRUD Operations

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

Now that we have our project set up and the database schema created, it's time to implement the CRUD operations. We'll create a simple REST controller that handles HTTP requests and responses.

First, create an interface 'BookRepository' that extends 'JpaRepository' to perform database operations:

Building Spring Boot, MongoDB and React.js CRUD Web Application – Djamware
Building Spring Boot, MongoDB and React.js CRUD Web Application – Djamware
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Top 22 Spring Boot Questions for Java Interviews
Top 22 Spring Boot Questions for Java Interviews
Spring Boot, MongoDB and Angular 5 CRUD Java Web Application – Djamware
Spring Boot, MongoDB and Angular 5 CRUD Java Web Application – Djamware
Djamware – Modern Programming Tutorials for Web & Mobile Developers
Djamware – Modern Programming Tutorials for Web & Mobile Developers
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Master CRUD Operations: Spring Boot and Spring Data JPA Unleashed
Spring Boot CRUD and JPA: Your Ultimate Book Example Guide
Spring Boot CRUD and JPA: Your Ultimate Book Example Guide
Top 10 Projects You can Build to Learn Spring Boot in 2025
Top 10 Projects You can Build to Learn Spring Boot in 2025
Building your first REST API: Java and Spring Boot
Building your first REST API: Java and Spring Boot

`public interface BookRepository extends JpaRepository { }`

Creating the Book

To create a new book, make an HTTP POST request to '/api/books' with the book details in the request body. In your controller, inject the 'BookRepository' and use it to save the book:

`@PostMapping("/api/books")
  public ResponseEntity createBook(@RequestBody Book book) {
    Book savedBook = bookRepository.save(book);
    return new ResponseEntity<>(savedBook, HttpStatus.CREATED);
}`

Reading Books

Implement HTTP GET methods to retrieve a list of all books or a single book by its ID:

`@GetMapping("/api/books")
  public List getAllBooks() {
    return bookRepository.findAll();
}
@GetMapping("/api/books/{id}")
  public ResponseEntity getBookById(@PathVariable Long id) {
    return bookRepository.findById(id).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}`

Updating a Book

To update a book, make an HTTP PUT request to '/api/books/{id}' with the updated book details in the request body:

`@PutMapping("/api/books/{id}")
  public ResponseEntity updateBook(@PathVariable Long id, @RequestBody Book updatedBook) {
    return bookRepository.findById(id).map(book -> {
      book.setTitle(updatedBook.getTitle());
      book.setAuthor(updatedBook.getAuthor());
      book.setPublisher(updatedBook.getPublisher());
      Book savedBook = bookRepository.save(book);
      return new ResponseEntity<>(savedBook, HttpStatus.OK);
  }).orElse(ResponseEntity.notFound().build());
}`

Deleting a Book

To delete a book, make an HTTP DELETE request to '/api/books/{id}':

`@DeleteMapping("/api/books/{id}")
  public ResponseEntity deleteBook(@PathVariable Long id) {
    bookRepository.deleteById(id);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}`

Testing the CRUD API

Now that you've implemented the CRUD operations, it's time to test your API. You can use tools like Postman or curl to make HTTP requests to your application. To run your Spring Boot application, use the following command in your terminal:

`./mvnw spring-boot:run`

Then, use your preferred tool to send HTTP requests to 'http://localhost:8080/api/books' and verify that the CRUD operations are working as expected.

And there you have it! You've just created a fully functional CRUD API using Spring Boot. As you continue your journey with Spring Boot, remember to explore features like Validation, Exception Handling, Security, and CORS to make your applications more robust and secure.