Create and deploy Java Spring Boot application to Heroku cloud platform

Start a new Spring Boot project

Either use Spring Boot CLI or go to https://start.spring.io/ to create a “web” project. In this step, I prefer the second option. In the “Dependencies” dialog search for and add the “Web” and “MySQL” dependency as shown in the screenshot. Hit the “Generate” button, download the zip, and unpack it into a folder on your computer.

Initialize Spring Boot Project

Open this project in Eclipse or IDE of your choice. I am using Eclipse. Import the above-downloaded project from File > Import, it will install all the dependencies and within a few minutes, you are ready to go.

Import the downloaded project

Create a package named “controller” by right-clicking src/main/java > New > Package

Create a package named controller

Then create a controller class “SpringQuestionController” by right-clicking controller package > New > Class and put request mapping for “/” and “/index”.

Here “/index” will return a view so the next step is to create an HTML file named index in src/main/resources/templates and put some content to display in the file.

Then add the dependency to parse HTML. Spring Boot provides auto-configuration for Thymeleaf by adding the spring-boot-starter-thymeleaf dependency.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Now run the application and navigate to any one of the two mapped path http://127.0.0.1:8080 or http://127.0.0.1:8080/index

The application is running as expected. Now lets deploy it to Heroku cloud platform. To do so create a free account on Heroku and download Heroku CLI and install it.

Now open up the integrated terminal (Ctrl+Alt+T) and login to Heroku by typing

heroku login

It will open a web page to login to your Heroku account. Open the web page and login, after successful login you can run heroku commands from the terminal.

To deply the code from local machine to Heroku, we need to create git repository of the project and push it to Heroku remote.

To initilize local git repo type following command at project’s base direcory

git init

It will initialize an empty git repository named spring-question-bank.git

Then add all the files of project directory to this empty repository

git add .

Then commit it

git commit -m “Initial commit”

Now create a new Heroku app

heroku create

It will create a new app with auto generated name and it also creates a remote git repository with the name “heroku”. You may later change app name (heroku apps:rename) based on availibility.

Now we need to push our master branch to heroku

git push heroku master

Heroku automatically detectst that it is a java application and starts the build. Once the build completes, it deploys by running the generated JAR files. Check the logs to verify if the application has started up in heroky by typing

heroku logs --tail

Now to view our application we need to type

heroku open

Our live application is open in the browser.

In the Heroku Dashboard our new app is visible now. Explore various settings to suit your requirement.

The source code is available at Github for you to refer to.

Connect: Twitter, GitHub, Linkedin

You can support by buying a coffee ☕️ here https://www.buymeacoffee.com/mdutt

Facilitator | Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store