BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Java
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
Javachevron-right

Getting Started With Azure Spring Apps and MongoDB Atlas: A Step-by-Step Guide

Maxime Beugnet5 min read • Published Jan 27, 2024 • Updated Jan 27, 2024
AzureSpringAtlasJava
FULL APPLICATION
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

Embrace the fusion of cloud computing and modern application development as we delve into the integration of Azure Spring Apps and MongoDB. In this tutorial, we'll guide you through the process of creating and deploying a Spring Boot application in the Azure Cloud, leveraging the strengths of Azure's platform, Spring Boot's simplicity, and MongoDB's capabilities.
Whether you're a developer venturing into the cloud landscape or looking to refine your cloud-native skills, this step-by-step guide provides a concise roadmap. By the end of this journey, you'll have a fully functional Spring Boot application seamlessly running on Azure Spring Apps, with MongoDB handling your data storage needs and a REST API ready for interaction. Let's explore the synergy of these technologies and propel your cloud-native endeavors forward.

Prerequisites

  • Java 17
  • Maven 3.8.7
  • Git (or you can download the zip folder and unzip it locally)
  • MongoDB Atlas cluster (the M0 free tier is enough for this tutorial). If you don't have one, you can create one for free.
  • Access to your Azure account with enough permissions to start a new Spring App.
  • Install the Azure CLI to be able to deploy your Azure Spring App.
I'm using Debian, so I just had to run a single command line to install the Azure CLI. Read the documentation for your operating system.
Once it's installed, you should be able to run this command.
It should return something like this.
Note: It's normal if you don't have the Spring extension yet. We'll install it in a minute.
You can log into your Azure account using the following command.
It should open a web browser in which you can authenticate to Azure. Then, the command should print something like this.
Once you are logged into your Azure account, you can type the following command to install the Spring extension.

Create a new Azure Spring App

To begin with, on the home page of Azure, click on Create a resource.
Create a resource
Then, select Azure Spring Apps in the marketplace.
Azure Spring Apps
Create a new Azure Spring App.
Create a new Azure Spring App
Now, you can select your subscription and your resource group. Create a new one if necessary. You can also create a service name and select the region.
Basics to create an Azure Spring App
For the other options, you can use your best judgment depending on your situation but here is what I did for this tutorial, which isn't meant for production use...
  • Basics:
    • Hosting: "Basic" (not for production use, but it's fine for me)
    • Zone Redundant: Disable
    • Deploy sample project: No
  • Diagnostic settings:
    • Enable by default.
  • Application Insights:
    • Disable (You probably want to keep this in production)
  • Networking:
    • Deploy in your own virtual network: No
  • Tags:
    • I didn't add any
Here is my Review and create summary:
Review and create
Once you are happy, click on Create and wait a minute for your deployment to be ready to use.

Prepare our Spring application

In this tutorial, we are deploying this Java, Spring Boot, and MongoDB template, available on GitHub. If you want to learn more about this template, you can read my article, but in a few words: It's a simple CRUD Spring application that manages a persons collection, stored in MongoDB with a REST API.
  • Clone or download a zip of this repository.
If everything went as planned, you should now have a JAR file available in your target folder named java-spring-boot-mongodb-starter-1.0.0.jar.

Create our microservice

In Azure, you can now click on Go to resource to access your new Azure Spring App.
Go to resource
Now, we can create our first microservice. Click on Create App.
Create app
Insert a name for your application, select Java 17, and click Create.
Go to resource
You can now access your new microservice.
Access your microservice

Environment variables

In our Java project, our application.properties file contains the following:
This means that our Spring application will try to find a MongoDB URI in the MONGODB_URI environment variable first, and will default to mongodb://localhost if it doesn't exist.
As we want to connect our microservice to our cluster in MongoDB Atlas, we have to set this environment variable correctly.
  • Create a new environment variable in your configuration.
Create a new environment variable
Note: If you don't have a database user, it's time to create one and use the login and password in your connection string.

Atlas network access

MongoDB Atlas clusters only accept TCP connections from known IP addresses.
As our Spring application will try to connect to our MongoDB cluster, we need to add the IP address of our microservice in the Atlas Network Access list.
  • Retrieve the outbound IP address in the Networking tab of our Azure Spring App.
Retrieve the outbound IP address
  • Add this IP address to the network access list in Atlas.
Add IP address in the Network Access list in Atlas

Deploy our microservice

We can now deploy our JAR file!
Click on the Apps tab and Deploy app.
Deploy app tab
You'll see the instructions to deploy your application.
Deploy app instructions
  • We have to set our Subscription ID:
  • We can deploy!
If everything went well, you should see some logs happily telling you that the Spring app fired up successfully and hooked up with our MongoDB Atlas cluster.
Microservice deployed

Assign an endpoint

Finally, we need to assign an endpoint to our service to start discussing with the REST API.
Assign endpoint
Retrieve the endpoint to test the REST API. For me, it's:
Retrieve endpoint

Test the REST API

You can create a person using the following cURL command. Don't forget to change the endpoint.
You can retrieve the persons from the MongoDB cluster in Atlas using the following cURL command.
Which should print something like this.
In our MongoDB Atlas cluster, we can confirm that the data was saved correctly.
persons collection in MongoDB Atlas
And because this project also uses Swagger and OpenAPI3, you can access the Swagger UI here:
Swagger UI

Conclusion

And that's a wrap! You've smoothly navigated the realms of Azure Spring Apps and MongoDB, crafting a dynamic Spring Boot application in the Azure Cloud. Your microservice is up, the REST API is ready, and MongoDB is seamlessly managing your data.
Consider creating a free Atlas cluster and start exploring all the features MongoDB Atlas has to offer.
Got questions or itching to share your success? Head over to the MongoDB Community Forum – we're all ears and ready to help!
Cheers to your successful deployment, and here's to the exciting ventures ahead! Happy coding! 🚀

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Code Example

Build a MongoDB Spring Boot Java Book Tracker for Beginners


Mar 22, 2023 | 0 min read
Tutorial

Connection to MongoDB With Java And SOCKS5 Proxy


Apr 17, 2024 | 2 min read
Quickstart

Java - Change Streams


Jul 12, 2024 | 11 min read
Tutorial

Java Aggregation Expression Builders in MongoDB


Apr 02, 2024 | 11 min read
Table of Contents