DEV Community

Cover image for Deploying Spring Boot Application on Heroku and connecting the app to MongoDB Atlas
Preethi Vuchuru27916
Preethi Vuchuru27916

Posted on

Deploying Spring Boot Application on Heroku and connecting the app to MongoDB Atlas

Hello Readers,

I recently tried to set up a Spring Boot application on Heroku that could connect to a MongoDB database hosted on MongoDB Atlas. Along the way, I faced several challenges that I wanted to document for future reference. I thought this information could also be helpful for other people who may be trying to do the same thing.

Here are the things I'll need for a quick deployment:

  • A text editor such as IntelliJ
  • A Spring Boot project
  • An account with MongoDB Atlas
  • An account with Heroku

I set up a sample Spring Boot project using Spring Initializr [start.spring.io].

Image description

Next, I added some simple dependencies like Spring Web, Spring Boot Dev Tools, and Spring Data MongoDB.

I created a simple REST service that only has one POST method to store details to the database.

Image description

Model

Image description

Repository
Image description

Controller
Image description

Deployment to Heroku

To deploy my application to Heroku, I followed these steps:

  • First, I created an account on Heroku.
  • Then, I created a new app by clicking on "New" and gave it a name and region.
  • Heroku automatically creates many things for us, but we only need to focus on the deploy option it provides for hosting applications.
  • To deploy my code to Heroku, I needed to have a GitHub repository to store it. This repository could either be my personal GitHub repository or a Heroku-provided GitHub repository that is created when the app is created.
  • I chose to use the Heroku-provided GitHub repository for pushing my changes.

Image description

To push changes to heroku git repository

git init
heroku git:remote -a sample-prooject
git add
git commit -m "initial commit"
git push https://git.heroku.com/sample-prooject.git

Now, whenever I push changes to the repository, Heroku automatically starts the build process to deploy the updated application.

Image description

Image description

Image description

However, I faced an error during the deployment process. The reason for this error was that Heroku, by default, compiles using Java 8, but my project was specified to use Java 17.

To resolve this issue and instruct Heroku to handle Java 17, I needed to add a file called "system.properties" in the root folder of my project. This file included the following line:

java.runtime.version=17

Image description

After adding and committing the changes to my repository, I pushed the changes to Heroku using the command "git push https://git.heroku.com/sample-prooject.git".

Image description

Next, I wanted to store my data in a cloud-based MongoDB database and decided to use MongoDB Atlas for this purpose.

To get started with MongoDB Atlas, I followed these steps:

  • I created an account on MongoDB Atlas.
  • I chose the M0 option for deploying the database, as it was free of cost.
  • I selected AWS as the provider and chose the nearest available region.
  • I named my cluster "Cluster".

Image description

  • After setting up the cluster, I created a database user for my MongoDB database.

Image description

  • Now I clicked on "Connect" in the MongoDB Atlas dashboard.

spring.data.mongodb.uri=mongodb+srv://preethi27916:<yourpw>@cluster.ogokpuw.mongodb.net/?retryWrites=true&w=majority

spring.data.mongodb.database=sampledb

  • Now I pushed my changes to heroku git to host the application.
  • Next, I added a connection IP address (i.e., the IP address from which I wanted to connect to this database) and clicked on "Connect to Application". [During the initial stages of setting up the connection between my application and database, I faced an issue where the connection was failing. After some investigation, I discovered that the Heroku-deployed application IP was not being recognized by MongoDB Atlas. To resolve this issue, I had to whitelist the IP address by specifying 0.0.0.0/0, which means that my database could be accessed from any IP address.].
  • MongoDB Atlas provided me with a connection string, which I needed to specify in the application properties file (located in the resources folder) of my Spring Boot project.

Finally, I tested my application by invoking the URL "https://sample-prooject.herokuapp.com/name/ihteerp" with a POST method.

Image description.

After testing my application and verifying that the data was being inserted into my MongoDB database, I can confirm that I have successfully completed the deployment of my application and database, and have set up the interaction between the two.

Top comments (1)

Collapse
 
soorajsnblaze333 profile image
Sooraj (PS)

Great post. Very informative and keep building more!