Introduction
In this tutorial, our focus is to start a beginner series using Spring Boot. This particular tutorial will cover creating a Spring Boot application using Maven. That said, let us understand what Spring Boot is.
Getting started with Spring Boot.
Spring Boot represents a way to create applications that conform to the proper separation of concerns. It integrates various technologies and ensures that projects are delivered quickly. Spring Boot projects are easily created using Spring Initializr.
Spring Initializr helps get multiple components needed to develop a spring boot application as quickly as possible. A central repository called Maven manages all dependencies and other specified project configurations.
In this tutorial, we would focus on developing a simple Hello World program using Spring Boot and Thymeleaf to manage the front end. Thymeleaf provides a way to present HTML in a dynamic form.
General Requirements
To get started with our development, the following system requirements are necessary:
- JDK 1.8 or above
- Maven 3.2 or above
- Thymeleaf
- Spring Boot
Annotations and their functions
In Spring Boot, annotations represent metadata that provides information about a program. It is a way of providing brief but vital information about components. Annotations are not a critical part of the application but are very intelligent additions that make it easier for code interpretation. In this tutorial, we used several annotations but we will not go into detail about them as this is simply a beginner series. However, we must add that annotations start with an “@” symbol.
Developing our Hello World Program in easy steps
Setting everything up
Using our Spring Initializr to get the basic structure needed for our application. The parent dependency is our spring boot code.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
</parent>
Our Thymeleaf and other dependencies used in the project include
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Putting our components together, we have the controllers as follows:
The main controller which has the configuration
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
}
From the above, the @SpringBootApplication is an annotation that bundles everything needed to get our spring boot project started.
The Helloworld class code is also shown below
@Controller
public class HelloSpring {
String Message;
@GetMapping("/")
public String GetHello(Model model){
Message = "Hello World";
model.addAttribute("Message", Message);
return "hello";
}
}
Our Html (frontend) serving page sample code is shown here
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Spring Boot Thymeleaf Hello World Example</title>
<link rel="stylesheet" th:href="@{webjars/bootstrap/4.2.1/css/bootstrap.min.css}"/>
<link rel="stylesheet" th:href="@{/css/main.css}"/>
</head>
<body>
<main role="main" class="container">
<div class="starter-template">
<p> <span th:text="${Message}"> </span> Welcome to Our Getting Started tutorial.</p>
</div>
</main>
<script type="text/javascript" th:src="@{webjars/bootstrap/4.2.1/js/bootstrap.min.js}"></script>
</body>
</html>
Conclusively, our program is able to show how to implement a Hello world application using Spring Boot. The complete source code for this project is available on our GitHub Page.