Build Spring Boot Application

In this post, we will create an application with Spring Boot. In the previous post, we understood how Spring Boot has revolutionalized building Spring applications. Spring Boot was developed to simplify the development of Java applications keeping in mind the modern paradigm of microservices.

In this series of Spring Boot Tutorial, we will be building an e-commerce store in which we will be able to add and order books. We will be using Spring, Spring Boot, and a variety of related libraries and frameworks to achieve the goal step-by-step.

Initialize Spring Application

To get started, direct to Spring Initializer here. You will see simple UI as shown below.

spring-initializr
Spring Initializr UI

In the UI, you will be asked very basic information:

  • Build Tool: Maven or Gradle
  • Language: Java, Kotlin or Groovy
  • Spring Boot: The version of Spring Boot you want to use.
  • Project Information: The group and artifact of the project.

Let us fill the basic information in the UI.

spring-initializr-info
Filling basic Info in Spring Initializr UI

We will change group and artifact id. We will also give a name to our Spring Application.

spring-project-info
Project Info in Spring Initializr UI

After filling up basic information required for an application we will start adding the dependency of the project.

spring-initializr-dependency
Adding dependency in Spring Initializr UI

We will select Web, Thymeleaf, DevTools and Lombok dependencies for our project.

spring-boot-application-dependency
Selected Dependencies for Spring Application

When we are finished, we will click the Generate button, to let Spring Initialitzr generate the project and download it as zip. We will unzip the project and load it in whatever IDE we want. For reference, I will be using IntelliJ IDEA.

After importing the project in IntelliJ, the project structure will look like this.

spring-boot-application-project-structure
Spring Application Project Structure

Within the project structure, we want to take note of these items:

  • mvnw and mvnw.cmd – These are maven wrapper scripts. You don’t need to have Maven installed on your system to build the applications. These scripts will help you to do so.

  • pom.xml – This is the Maven build specification.

  • BookStoreApplication – This is the Spring Boot main class that bootstraps the application.

  • application.properties – This file initially will be empty. We will add all the configurations of our project in this file.

  • static – In this folder, you will place static resources like CSS, javascript, images, etc.

  • templates – In this folder, you will place UI templates that will be used to render content in the browser. We will be using Thymeleaf for this purpose.

  • BookStoreApplicationTests – This is a simple test to ensure that Spring Application Context loads successfully. We will add more tests on the go.

Bootstrap the Application

Now, let us take a closer look into BookStoreApplication.

In the above example, you will notice @SpringBootApplication in the main class. It clearly signifies that it is a Spring Boot Application. But it is not there just to signify. There is more to it. @SpringBootApplication combines 3 other annotations.

  • @SpringBootConfiguration – This designates class as a configuration class. This annotation is a special form of @Configuration annotation.

  • @EnableAutoConfiguration – This enables Spring Boot automatic configurations. It tells Spring to automatically configure beans and wire them automatically that it thinks you’ll need.

  • @ComponentScan – This enables component scanning in your project. Any class annotated with @Component, @Service, @Controller, etc. will be registered as bean by the Spring in its application context.

The main() method calls a static run() method on the BookStoreApplication class, which performs the actual bootstrapping of the application, creating the Spring application context. The two parameters passed to the run() method is a configuration class and the command-line arguments.

We are just getting started so we will be starting off with small changes in Book Store Application. We will be creating a homepage for our application. To add homepage we have to do the followings:

  • A controller class that will handle the request for homepage.
  • A view template that defines how the homepage will look.

Handling Web Request

We will be using the Spring MVC module to handle web requests. Controllers are the core concept of the Spring MVC. Controllers are the class that accepts the web request and respond with information. In the browser-based application, the controller responds by population model data and passes the request to view resolver to generate HTML which will be returned to the browser finally. Let us see the simple controller class.

The class is annotated with @Controller. Spring’s component scanning will automatically discover it and creates a bean of HomeController in the application context.

The home() method is annotated with @GetMapping to indicate that if an HTTP GET request is received for the root path /, then this method should handle that request.

Creating the View Template

Let us define a very simple homepage that welcomes users to their site. We will be defining the basic Thymeleaf template for this purpose. We’ll create the template in our project at /src/main/resources/templates/home.html. Let’s create that template now.

In the example, inside the <img> tag, we use thymeleaf th:src and @{..} expression to reference the image. The image is referenced with context relative path /image/book-store-welcome.jpg. All the static content is kept in the /src/main/resources/static folder. That means that the BookKart logo image must also reside within the project at /src/main/resources/static/images/book-store-welcome.jpg.

Now we have got a controller to handle the request for the homepage and a view template to render the homepage, we are ready to fire up our application and see it in action.

Running the Application

Just like any other Java Application, We will run the main function of BookStoreApplication in the IntelliJ. The application starts right up.

spring-boot-application-console
Start the Spring Application

You’ll see a log entry saying Tomcat started on port(s): 8080 (http), which means that you’re ready to point your web browser at the homepage to see the welcome page.

If you’re wondering how did Tomcat start? Here’s the answer. The Spring Boot brings everything it wants with it. You don’t need to deploy it in any application server.

You never deploy your application to Tomcat… Tomcat is a part of your application!

Now that the application has started, point your web browser to http://localhost:8080.

spring-boot-application-homepage
The BookKart homepage

Congratulations!! Our first Spring Boot application is up and running now.

Conclusion

Let us summarize what we have done until now to reach this point.

  • We created the initial project structure using Spring Initializr.
  • We wrote a controller class to handle the request of the homepage.
  • We defined the Thymeleaf template to render the homepage.

Our Spring journey has just begun. The BookKart application only touched on a small portion of what Spring has to offer. We will further see how we can add functionalities to our BookKart Applications with the help of other Spring functionalities in upcoming posts.