This repository showcases how to integrate MongoDB with Spring Data, covering essential practices for creating complex queries and optimizing performance. It includes sample code that guides you through working with Spring Data to effectively handle data operations and improve query efficiency.
You can read more on:
- Java 17 or higher (tested with Corretto 21)
- Gradle
- MongoDB running locally or in the cloud (e.g., Atlas)
You can run the application in two ways:
- Open the project.
- Run the
SpringShopApplication.java
class.
export MONGODB_URI="<YOUR_CONNECTION_STRING>"
./gradlew bootRun
When the application starts, it automatically loads:
-
15 Customers with realistic names and global cities
-
10 Transactions including different types (Credit, Debit), statuses (PENDING, COMPLETED, ERROR), and account details
Data is only inserted if the collections are empty to avoid duplicates.
Once the application is running, access:
http://localhost:8080/swagger-ui.html
Or (for some versions):
http://localhost:8080/swagger-ui/index.html
It includes endpoints for:
- /customers → Manage customer records
- /transactions → Manage financial transactions
Create a Customer
POST /customers
Search Transactions
GET /transactions/search?type=Credit&status=PENDING
Export Error Transactions
POST /transactions/actions/export-errors
This project includes ready-to-use .http files to test all API endpoints easily from your IDE (such as IntelliJ IDEA, Rider, or VSCode with REST Client plugin).
- customers.http → Test all endpoints under /customers
- transactions.http → Test all endpoints under /transactions
- Open the .http file in IntelliJ.
- Click on the green "Run" icon next to any request.
- You’ll see the response in a built-in HTTP client tab.
This project uses JUnit for testing and JaCoCo for code coverage.
To execute all unit and integration tests in the project:
./gradlew test
After the command runs, open the HTML report:
/build/jacocoHtml/index.html