A simple CRUD-based RESTful API built with Spring Boot that connects to a MySQL database.
This project demonstrates how to create, read, update, and delete Book entities using Spring Data JPA.
It also includes a Postman collection to help you quickly test the endpoints.
- Features
- Technologies Used
- Prerequisites
- Getting Started
- Database Configuration
- Postman Collection
- Project Structure
- Contact & Blog
- CRUD Operations for managing
Bookentities - Spring Data JPA for easy database interaction
- MySQL running on Docker (local DB optional if you prefer)
- REST Endpoints that can be tested with Postman or any REST client
- Java 17 (or compatible version)
- Spring Boot 3.x (Web, Data JPA)
- MySQL (via Docker)
- Maven (build tool)
- Postman (for testing; optional)
- Java (version 17+ recommended)
- Maven (to build and run the project)
- Docker (if you want to run MySQL in a container)
- Git (for cloning this repository)
- Clone the Repository
git clone https://github.com/sahandilshan/spring-boot-rest-api-demo.git cd spring-boot-rest-api-demo - Run MySQL in Docker (Optional) If you don’t have MySQL installed locally, spin up a container:
docker run -d \ --name dev-mysql \ -e MYSQL_ROOT_PASSWORD=secret \ -e MYSQL_DATABASE=mydatabase \ -p 3306:3306 \ mysql:8
- Configure Application Properties Check src/main/resources/application.properties to ensure the database URL, username, and password match your MySQL instance.
- Build and Run the Application
mvn spring-boot:run
The application will start on http://localhost:8080.
In application.properties, you can customize the following properties:
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=trueA Postman collection is provided in the src/main/resources folder: Book API.postman_collection.json
- Open Postman and click Import.
- Choose the file Book API.postman_collection.json.
- You’ll see the collection with pre-configured endpoints under Collections in Postman.
- Make sure your server is running locally on port 8080.
- Send requests to create, get, update, or delete book records!
The project is structured as follows:
spring-boot-rest-api-demo
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com.example.springbootrestapi
│ │ │ ├── SpringBootRestApiApplication.java
│ │ │ ├── controller # RestControllers (e.g., BookController)
│ │ │ ├── entity # JPA Entities (e.g., Book)
│ │ │ ├── repository # JPA Repositories (e.g., BookRepository)
│ │ │ └── service # Services (e.g., BookService)
│ │ └── resources
│ │ ├── application.properties
│ │ └── Book API.postman_collection.json
├── pom.xml
└── README.md- Author: Sahan Dilshan
- Blog Post: For a detailed walkthrough of this project, check out the blog post explaining the setup, structure, and how each class works.
- Contributions: Feel free to fork this repo, open issues, or submit pull requests!