How to Set Up and Deploy a Java Spring Boot Application

April 25, 2022
Written by
Diane Phan
Reviewed by

header - How to Set Up and Deploy a Java Spring Boot Application

It might seem intimidating to build projects using Java especially when it seems confusing to know how to start and set up. Fortunately, using the Spring Boot and ngrok tools will reassure you that writing code for Java applications can be fast and seamless.

Spring Boot reduces the problem of figuring out how to structure the project hierarchy and provides sufficient documentation for developers to create standalone applications without relying on external web servers.

Many articles on the Twilio blog utilize ngrok, a localhost tunneling tool that exposes webhooks on public URLs that can be reached by Twilio. The excellent ngrok tool is also used to generate temporary public URLs for your development web server.

In this article, you will learn how to set up a Java Spring Boot application and create a tunnel from a URL on the domain to the local application running on your computer.


Create and import Spring Boot project

This project utilizes a resource called Spring Intializr. This tool generates the basic structure of a Spring Boot project for developers to get started quickly.

Go to the Spring Initializr site. Under Project, choose “Maven” and then “Java” as the language. Note that this tutorial is built with Spring Boot version 2.6, so select the same version in Spring Initializr.

For the Project Metadata, change the name of the project listed in the Artifact section. Name this application artifact "springbootapp". Give the Name section the same name, as this is the display title that creates the entry point for the project.

Feel free to add in a description for the project to help you organize better.

Choose “Jar” as the Packaging type as the application will run in the embedded Tomcat server provided by Spring Boot, as well as Java version 11.

Refer to the image below to ensure your Spring project matches up.  

The spring initializr form with Maven selected as the project type, Java as the language, 2.6.6 as the Spring Boot version, Jar as Packaging, and 11 as the version of Java. The artifact text field is set to "springbootapp".

On the right-hand side, click on the Add Dependencies button and search for "Spring Web" under the Dependencies section.

"spring web" on the list of dependencies for spring boot initializer

Click on the Generate button at the bottom of the page and the browser will download a .zip file containing the boilerplate code for the project. Extract the .zip file.

Open IntelliJ IDEA and open the root folder of the extracted contents from the folder. Click on Finish to import your project into your IDE.  

Explore the file structure

Examine the project directory and expand the src/main/java folder to see the com.example.springbootapp subfolder. The file structure should look similar to the screenshot below:

The file structure of the project with a subfolder "com.example.springbootapp" in the "src/main/java" folder.

Expand the com.example.springbootapp package to reveal a class file named SpringbootappApplication. This class exists to compile and execute the code later on in the article.

Notice that the pom.xml folder was also generated for your convenience with the information regarding the "Spring Web" dependency. The pom.xml file stands for Project Object Model which is essential for Maven projects as it consists of details used to build the project. If you added more dependencies, they would be listed in this file as well.

Build the web controller

A controller is required in Java projects to handle the HTTP requests made from the user to the web application. In this case, the user will make a request to the Spring Boot application website and view a message on their web browser.

Right click on the com.example.springbootapp package to create a new Java class named

Copy and paste the following code to the newly created file:

package com.example.springbootapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

public class WebController {
    public String helloWorld() {
        return "Hello world I'm on the web!";

The @RestController annotation marks the request handler required to build and provide the RESTful web service during runtime. The only endpoint in this service is mapped to the "/" route.

Feel free to change the return string message. Save the file.

Open the ngrok tunnel

After defining the web controller, the ngrok client and tunnel needs to be initialized.

As mentioned, ngrok is a handy utility to connect the development version of the Java application running on your system to a public URL that Twilio can connect to.

Unfortunately, ngrok no longer allows users to anonymously serve HTML content with an ngrok URL. Therefore, you are required to sign up for an ngrok account to generate an authtoken. Download ngrok onto your machine and read the instructions to configure the ngrok authtoken.

Navigate to the file. Import the following lines to the top of the file:

import com.github.alexdlaird.ngrok.NgrokClient;
import com.github.alexdlaird.ngrok.protocol.CreateTunnel;
import com.github.alexdlaird.ngrok.protocol.Tunnel;
import org.springframework.boot.SpringApplication;

Copy and paste the following highlighted code inside the void main() function:

        public static void main(String[] args) {
      , args);
                final NgrokClient ngrokClient = new NgrokClient.Builder().build();
                final CreateTunnel createTunnel = new CreateTunnel.Builder()
                final Tunnel tunnel = ngrokClient.connect(createTunnel);

The final keyword is added to restrict the variables from being modified. The withAddr() object method is added to specify the port number for the Spring Boot application to run on.

You can view the documentation for declaring and using the ngrok client in the java-ngrok GitHub README.

Navigate to the pom.xml file to add the following dependency to the list of dependencies:


Look at the top-right corner of the IntelliJ IDEA window and find the little icon with an "M" shape with a little blue recycle logo on top. Click it to load Maven changes.

An icon with an "M" shape with a tooltip "Load Maven Changes"

In order for the ngrok tunnel to be opened successfully, the port must be changed to port 8080 to match the tunnel address established in the file. Navigate to the src/main/resources/ subfolder and open the file. Add the following line and save:


Run the spring boot application

In the file, click on the green play button next to the public class definition and select the Run option as seen below:

Users clicks on the green triangle next to the "LunchOrdersApplication" class. A context menu with "Run "SpringbootappApplication"" appears.

Wait a few seconds for the project to build, download the project's dependencies, and compile.

Soon, the terminal should display a link to port 8080 as well as a randomly generated URL link. You may have to scroll down the output text for a bit until you find a URL similar to “”. Here's an example of the output you might have to scroll through:

The output of the application with logs that contain the ngrok Forwarding URL

NOTE: Keep in mind that this URL is no longer active and only presented here for demonstration purposes.

Click on the ngrok URL to be redirected to the web browser. The output should be the same as clicking on the localhost:8080 URL, as seen below:

message on the local host web browser saying "Hello world I&#x27;m on the web!"

Make sure that you are using an ngrok account if you are having trouble seeing the string message on the web browser.

What's next for building Java Spring Boot projects?

Congratulations on setting up and deploying a Java Spring Boot application with ngrok! Now that the application is exposed publicly on the web, you can integrate webhooks into your project and share the application with other users.

Check out the following articles to quickly integrate Twilio and more to your Spring Boot application:

Diane Phan is a software developer on the Developer Voices team. She loves to help programmers tackle difficult challenges that might prevent them from bringing their projects to life. She can be reached at dphan [at] or LinkedIn.