DEV Community

Coumarane COUPPANE
Coumarane COUPPANE

Posted on

πŸš€ How to Use CosmosDB with Java Spring Boot API – A Complete Guide

πŸ“Œ Introduction
If you're a Java developer looking for a practical way to integrate Azure CosmosDB into your Spring Boot API, this open-source project is for you! πŸš€

I built this project to help developers who need a working example of using Spring Boot with CosmosDB. Whether you're learning NoSQL databases, working with Azure Cloud, or just want to see how CosmosDB works in a real-world application, this repo will help you.


🎯 Why I Built This Project
While working with CosmosDB, I realized that many developers struggle with:
βœ… Setting up CosmosDB in a Spring Boot application
βœ… Configuring Reactive Spring Data CosmosDB properly
βœ… Handling CRUD operations in a scalable way

So, I built this fully working API with CosmosDB to make it easy for developers to get started. The project is free, open-source, and easy to install!


πŸ“Œ Features
βœ… Spring Boot + Reactive CosmosDB Integration
βœ… CRUD API for Users (Create, Read, Update, Delete)
βœ… Pagination & Querying CosmosDB


πŸ› οΈ Prerequisites
Before you begin, ensure you have the following installed:
βœ”οΈ Java: Version 21 or later
βœ”οΈ Maven: Version 3.9.9 or later
βœ”οΈ Azure Subscription: Required for creating & accessing Azure CosmosDB
βœ”οΈ Azure CLI (az): Required for managing CosmosDB


πŸš€ How to Install and Run Locally
Follow these steps to clone and run the project:

# 1️⃣ Clone the Repository
git clone https://github.com/coumarane/spring-boot-cosmosdb.git
cd spring-boot-cosmosdb

# 2️⃣ Build the Project
./mvnw clean install

# 3️⃣ Run the Application
./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

πŸ”Œ Configure Azure CosmosDB Connection

Create a CosmosDB account before continuing with the further steps.

Use a Real CosmosDB Instance
Update an application.properties file in src/main/resources/ :

azure.cosmos.connection-string=<<PRIMARY CONNECTION STRING>>
azure.cosmos.database=<<COSMOSDB DATABASE>>
Enter fullscreen mode Exit fullscreen mode

πŸ“‘ API Endpoints & Testing

Once the application is running, you can test the APIs:

βœ… Get All Users

curl -i http://localhost:8080/users
Enter fullscreen mode Exit fullscreen mode

βœ… Create a New User

curl -i -X POST -H "Content-Type: application/json" \
  -d '{"email":"john@gmail.com", "firstName":"John", "lastName":"Doe", "city":"New York"}' \
  http://localhost:8080/users
Enter fullscreen mode Exit fullscreen mode

βœ… Update a User

curl -i -X PUT -H "Content-Type: application/json" \
  -d '{"email":"john_updated@gmail.com", "firstName":"John", "lastName":"Doe", "city":"Los Angeles"}' \
  http://localhost:8080/users/{USER ID}
Enter fullscreen mode Exit fullscreen mode

βœ… Delete a User

curl -i -X DELETE http://localhost:8080/users/{USER ID}
Enter fullscreen mode Exit fullscreen mode

🌟 GitHub Repo
πŸ”— Check out the full code here:
➑️ GitHub Repository: https://github.com/coumarane/spring-boot-cosmosdb

If you find this useful, please ⭐ star the repo and share it with others! πŸ˜ŠπŸš€


πŸ“ Final Thoughts
This project is open-source and meant to help developers quickly integrate CosmosDB with Spring Boot. If you're working on a cloud-based, NoSQL application, this will save you a lot of setup time!

Let me know what you think in the comments! πŸ’¬

πŸš€ Happy Coding!

Top comments (0)