Creating A Simple Spring Boot Application

Seyed Sahil
3 min readAug 29, 2020

Welcome to my new tutorial on Spring Boot web application development. In this tutorial, I will show you how to quick start with a simple Spring Boot web application that displays a welcome message when you hit the localhost URL.

Downloading Development Kit…

  • JDK-8 is recommended for this project
  • Download Amazon Corretto Open JDK from here
  • Configure the path variable JAVA_HOME to the JDK path

Downloading Spring Tool Suite…

  • Get the latest version of Spring Tool Suite IDE
  • Extract the zip file and prepare the IDE

Project Configuration…

For this tutorial, I will be using Maven dependency management system and so this will be a maven project.

By default, the Spring Boot has an embedded Apache Tomcat server configured. So you can directly run the application and server will be up and running in no time.

Note: It is possible to change the embedded Tomcat server.

Initialize the Web Application…

In this tutorial, I will be using two dependencies

  • Spring Web (Dependency added fore web app feature support including the Apache Tomcat default embedded container)
  • Thymeleaf (This is a templating engine)

Steps

  1. Go to Spring Initializr website.
  2. Choose the above-mentioned dependencies
  3. Set the defaults for configuration and change the project information.
  4. Click on generate project and it will generate a Maven project for you.
  5. Extract the zip file and import the project as a Maven project to your IDE.

Project Structure…

Starting the Web Application…

  1. Right-click on the Project
  2. Run As -> Spring Boot Application

This will bring up the server and you can access the web application. By default, the application will run on port 8080. So to access the web application you have to form the below-given URL.

http://localhost:8080/

Once you hit the URL, you will see the below Whitelabel error

Spring Boot will show a default Whitelabel error page when an error occurs. In this case, it is displayed for HTTP-404 (Not Found).

This means that the default root URL ‘/’ mapping is not defined in our web application. So our next step is to define that. So when the user accesses the URL, it will return an HTML page with a message.

Create the HTML Page…

Note: Create the HTML file under src/main/resources/templates folder

Create the Controller…

To map the root URL to the index page we have to define a mapping.

Steps

  1. Create a new class and annotate with @Controller
  2. Define a new mapping inside the web controller

Now if you run the application again and hit the URL, you will see the welcome page.

Role of Template Engine…

Here we have added the thymeleaf dependency and so Spring Boot will auto-configure this in our project. By default, the templates defined in the src/main/resources/templates folder will be picked up.

You can read more about auto-configuration here.

If you need additional info on how to deploy the web app in Docker check this URL

End of Tutorial

Thank you for reading this tutorial on Spring Boot web application development. If you like to read more articles like this, please let me know in the comments.

Get the complete source code from my GitHub now.

I’m waiting for your suggestions and feedback 🙂

Thanks

Seyed Sahil

Originally published at http://sydlabz.wordpress.com on August 29, 2020.

--

--

Seyed Sahil

Coding Since 2011, Software Engineer, Game Developer, Artist, Photographer. Passionate about Security and Web Technologies. Favourites — C, Java, Javascript.