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!

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)