Spring Boot Application Deployment on Heroku

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.
Date: 2021-05-04
Deploying a Spring Boot Application to Heroku: A Comprehensive Guide
This article explains how to deploy a simple Spring Boot application to the Heroku platform. We'll cover creating a basic Spring Boot application and then deploying it to Heroku, a cloud platform known for its ease of deployment and scalability. No prior experience with Heroku is assumed; this guide is designed to be accessible to beginners.
First, let's understand Spring Boot. Spring Boot is a framework built on top of the Spring framework, designed to simplify the creation of stand-alone, production-grade Spring-based applications. It streamlines the development process by automating configuration and providing a convenient way to build microservices. A key advantage is its ability to embed a server, eliminating the need for separate deployment of a web server like Tomcat. This makes deployment significantly easier.
Building the Spring Boot Application
The creation of our Spring Boot application involves several steps. First, we define the project's dependencies. This is typically done through a file named pom.xml (Project Object Model), which lists all external libraries the application requires. In this case, we would specify dependencies for Spring Boot itself and Lombok, a library that simplifies Java code by automatically generating boilerplate code (like getters and setters for class attributes).
Next, we create a configuration file, often named application.properties, to store application-specific settings. This might include database connection details, server port numbers, and other configurable parameters. The exact details in this file can be tailored to the application's needs.
The core of our application consists of several Java classes. The main application class, conventionally named something like SpringbootHerokuApplication.java, serves as the entry point. This class is annotated with @SpringBootApplication, a Spring Boot annotation that indicates it's the main application class, and contains the main method from which the application starts.
Additionally, we have a controller class, perhaps called SomeController.java, which handles incoming HTTP requests. In this example, we create two simple endpoints: one that displays a basic "index" message and another that returns the current server time. These endpoints are defined using annotations that map HTTP requests (like GET requests) to specific methods within the controller class. This defines how the application responds to requests from web browsers or other clients.
Running the Application Locally
Before deploying to Heroku, it's essential to test the application locally. This involves running the main application class (e.g., SpringbootHerokuApplication.java) as a Java application. Once running, the application listens on a specified port (in this case, port 9091), making the defined endpoints accessible for testing through a web browser.
Deploying to Heroku
Deploying to Heroku involves using Git and the Heroku command-line interface (CLI). Git is a version control system, and the Heroku CLI allows interaction with the Heroku platform from the command line. A Heroku account is also necessary; creating one is a straightforward process on their website.
After installing the Heroku CLI, you log into your Heroku account using the heroku login command. This will open a web browser and prompt you for your credentials, verifying your identity with Heroku.
Next, we initialize a Git repository in the project directory using standard Git commands (git init, git add ., git commit -m "Initial commit"). This creates a local repository to track changes in the application code.
Creating the Heroku App
To deploy to Heroku, we need to create a Heroku app. This is done using the heroku create command. This command creates a new application on Heroku and automatically links it to a Git remote repository. Heroku assigns a random, unique name to the app by default. You can customize this name by providing a desired name as an argument to the heroku create command (e.g., heroku create my-spring-app).
Deploying the Application
Finally, the application is deployed using the git push heroku master command. This command pushes the local Git repository to the Heroku remote repository. Heroku automatically detects the project type (in this case, a Java/Maven project due to the presence of the pom.xml file) and triggers the build process. This process builds the application, installs dependencies, and deploys it to Heroku's servers. The deployment time may vary depending on factors like network speed. You can monitor progress through Heroku's logs.
Verifying the Deployment
After successful deployment, Heroku provides a URL for your application, which is displayed in the logs following deployment. This URL allows accessing the application's endpoints. In our example, we can test the endpoints by accessing URLs like https://YOUR_APP_NAME.herokuapp.com/api/ (for the index message) and https://YOUR_APP_NAME.herokuapp.com/api/time (for the current time). The application's availability can also be verified by logging into your Heroku account dashboard.
Conclusion
This guide outlines the entire process of creating a basic Spring Boot application and deploying it to Heroku. By following these steps, developers can easily take advantage of Heroku's infrastructure to host and manage their Spring Boot applications, benefiting from Heroku's scalability, ease of deployment, and other features. Remember, this is a simplified example; more complex applications might require additional configuration and adjustments. However, this process provides a solid foundation for deploying more intricate applications in the future.