Sunday 9 October 2022

Spring Boot API with MongoDB Atlas

In this blog, We are going to explore and learn the Spring Boot application with MongoDB Atlas. For that, we need to first need to create an account at https://cloud.mongodb.com, and configure some default settings in order to spin a new free cluster. We also get the option to select the cloud provider and region.

 


after creating the cluster and configuring credentials, we are all set as you can see I have created a new cluster in the AWS cloud, and the Collection is called "task" and it is a shared one so free no charges will apply. 


Next, let's initialize the Spring Boot application for that, we can go to https://start.spring.io/  or if you have a Spring Boot plugin in your IDE you can use that as well.



In the Spring initializer, I have added four dependencies. 
  1. Spring Web:- For building RESTful APIs.
  2. Spring Data MongoDB: -  It is a part of the Spring Data project, which provides integration with the MongoDB document database.
  3. Spring Boot Actuator:- it is a sub-project of Spring Boot and is used for monitoring purposes.
  4. Lombok:- For Java annotations. 
After this, We can generate the code and open it in your favorite IDE. I have written code for CRUD operations to test the API with MongoDB Atlas.

Let's see 1-2 examples.
  • Create some content in MongoDB Atlast using Spring Boot API:

  • Let's invoke the GET by ID endpoint

  • GET ALL endpoint

  • Rest of the operations you can check in the code but one more thing I wanted to show here is how the data is stored in the MongoDB Atlas, for that we can go to the cluster and click on Browse Collection 

There are other things that we can explore such as Realtime Monitoring, Enabling Data API which is available in MongoDB Atlas.


  
 That's it in this blog. Code Repo.


Happy Coding and Keep Sharing!!

No comments:

Post a Comment