Deploying Spring Boot Application on Heroku and connecting the app to MongoDB Atlas
Tags: todayilearned, spring, heroku, mongodb
Published: 2023-03-01 23:32:55


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2at6cdcr5hsn1xuma5w.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/juoedggrkkix0ajpalxf.png)

**Model**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9quh55e4r010m9i6e9uy.png)



**Repository**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in7lozi124mr794gk7j0.png)

**Controller**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsi6wp45ruzw0pazuq8u.png)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qaqjtcsl25uyoddgth1w.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjutwlogipwyhl8lh3tw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/733pfrxj2wxsujhle0ey.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5ncpd3bn203ateo3v9m.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efw1maykoe47atib6dm3.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eispl5zbsuuon1igikj2.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtb428fusrue6my9itsz.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zs1q5327hrvzq78ehaf.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pipbdjfo9wexsxusxmxt.png).

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.