
Create a CRUD API with Spring Boot
The CRUD operations (create, read, update, delete) are the basic functionality of any web application when working with a database. This example will show you how to create the CRUD API with Spring Boot and use MySQL as a database.
Prerequisites
- JAVA 17
- Maven
- MySQL
Setup project
Create a testing database named "example" and run the database.sql file to import the table and data.
Project structure
Project files
pom.xml
This file contains the configuration and dependencies of the Maven project.
application.properties
This file contains the database configuration.
App.java
This file is the main entry point for the Spring Boot application.
ProductRepository.java
This file defines the product repository by utilizing the JpaRepository class, which implements the CRUD operations feature, so we can use it to perform any CRUD operations with less effort.
Product.java
This file defines the product entity that maps to our database table named "Product".
We use the lombok
library features to reduce the amount of code written for our entity by using @Getter @Setter @NoArgsConstructor
ProductService.java
This file is the Service Layer that contains your business logic and the data access operations. We define all CRUD methods here.
ProductController.java
This file defines all functions required to handle incoming requests and perform any CRUD operations.
We create routing URLs by using @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
annotations with the URL string as a parameter.@RequestBody
parses the incoming request body as an entity object.@PathVariable
maps a parameter from the URL.
We use ProductService
to perform any CRUD operations.
index.html
This file will be used to create a basic user interface for testing our API.
- Many other articles will use Postman as the HTTP client to test the API, but in this article, I will use JavaScript instead. This will help you understand more details when working with HTTP request on the client-side.
- To keep this file is clean and readable, we will only use basic HTML and JavaScript. There are no additional libraries such as the CSS Framework or Axios here.
- All CRUD functions will use the appropriate HTTP method to invoke the API.
showResponse(res)
will format the JSON object to make it easier to read.
Run project
Open the web browser and goto http://localhost:8080
Testing
Get All Products
Click the "Get Products" button. The API will return all products data.

Get Product By Id
Click the "Get Product" button and enter "1" for the product id. The API will return a product data.

Create Product
Click the "Create Product" button and enter "test-create" for the product name and "100" for the price. The API will return a newly created product.

Update Product
Click the "Update Product" button and enter "101" for the product id and "test-update" for the name and "200" for the price. The API will return an updated product.

Delete Product
Click the "Delete Product" button and enter "101" for the product id. The API will return nothing, which is acceptable as we do not return anything from our API.

Conclusion
In this article, you have learned how to create and setup the Spring Boot application in order to create a CRUD API. Utilize the JpaRepository class as an ORM to perform the CRUD operations on the database. Test our API using JavaScript. I hope you will enjoy the article.
Source code: https://github.com/stackpuz/Example-CRUD-Spring-Boot-3
Create a CRUD Web App in Minutes: https://stackpuz.com