DEV Community

Zar Li Hnin for Rey Tech Inc.

Posted on

Spring Boot Hello World Example by using Spring Initilizr

Introduction

In this article, you'll be able to start up your Spring Boot application and show Hello World and a message on your browser.

There are four ways to create Spring Boot project which are:

  1. Spring Initializr
  2. Spring Tool Suite (Spring Boot Starter Plugin)
  3. Simple Maven Project
  4. CLI (Command Line Interface)

If you want to create a project easily and quickly, the best ways are using 1. Spring Initializr or 2. Spring Tool Suite. In this tutorial, we will show you how to create Spring Boot project using Spring Initializr.

What is Spring Initializr?

The Spring Initializr is a web-based tool provided by the Pivotal Web Service. You can easily generate the structure with the help of Spring Initializr and you can add any dependencies and necessary configuration. After that, you can get a basic project structure and either a Maven or a Gradle build specification to build your code with.

IDE

You can use either IntelliJ, Eclipse, or STS.

Source Code

You can refer to the source code below.
https://github.com/ZarLiHninn/Spring-Boot-Hello-World-Example

Let’s start to show Hello World with Spring Initializr

Step 1

You can create a simple Spring Boot project by visiting the following site.
https://start.spring.io/
Alt Text

  • Choose your build tool: Maven or Gradle.
  • Choose Java Language to create Java web-based applications.
  • Choose Spring Boot version (2.3.1 version in this case)

Step 2

Alt Text

  1. Configure Group for your component which is the root package name to use.
  2. Configure Artifact for your component which is the name of the project. When configuring Artifact, you will be noticed the project name is automatically filled. Alt Text
  3. Fill description for your project.
  4. Fill the root package of the project. If not specified, the value of the Group attribute is used.
  5. Choose Jar or War.
    • If you want to execute Java application containing Java class files, and associated metadata, you can choose Jar.
    • If you want to develop Web application containing Servlet, JSP, HTML, JavaScript, and other necessary files, you can choose War.
  6. Choose Java Version you want to use.

Step 3

Spring Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the setup for you.
Alt Text
Choose Spring Web, Spring Boot DevTools, and Thymeleaf by clicking Add Dependencies.

  • The spring-web dependency contains common web-specific utilities for both Servlet and Portlet environments, while spring-web MVC enables the MVC support for Servlet environments.
  • The Spring-boot-devtools automatically reload your project when you change some code in your classpath file.
  • Thymeleaf is a Java-based library or template engine to create web-based applications.

Step 4

Alt Text
You can get the following project zip file by clicking the GENERATE button.
Alt Text

Step 5

Now, you can import your project zip file in your IDE (e.g. Eclipse, Intellij). In this tutorial, we will use Eclipse IDE and you need to follow these steps to import your project file.

  1. Click File->Import. Alt Text
  2. Search and select Existing Maven Projects and after clicking Next, browse the project zip file. Alt Text Now, we have imported project completely and you can see the project in project explorer. Alt Text

DemoApplication.java

  • It is the main class of Spring Boot Project.
package com.reytech.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

pom.xml

  • As we defined in Spring Boot Initializr GUI, you can see the following properties, description, dependencies, etc.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.reytech</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DemoApplicationTests.java

  • It contains the basic context that can be used to start writing the tests as we start developing the application.
package com.reytech.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

Step 6

Let’s add HomeController.java, hello.html and message.html to show Hello World and some message on your browser.

  • Under the demo package, please create a controller package and HomeController.
  • Under src/main/resources/templates directory, create hello.html and message.html. Alt Text

HomeController.java

  • hello() function calls hello.html when URL is localhost:8080/
  • message() function calls message.html and you can set the custom message on the controller by using model.addAttribute("message", "This is a custom message");
package com.reytech.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String hello(){
        return "hello";
    }
    @GetMapping("/message")
    public String message(Model model) {
        model.addAttribute("message", "This is a custom message");
        return "message";
    }
}

hello.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Demo Project</title>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

message.html

  • The th:text=”${attributename}” tag attribute is used to display the value of model attributes.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Demo Project</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html> 

Step 7
Just run the server and navigate to http://localhost:8080/
Alt Text
And navigate to http://localhost:8080/message.
You can see the following message on your browser.
Alt Text

Thank You.

References:

https://subscription.packtpub.com/book/application_development/9781789132588/1/ch01lvl1sec10/spring-initializr

https://www.javacodegeeks.com/2018/02/create-spring-boot-project-spring-initializer.html

Top comments (1)

Collapse
 
chacalonchacaloso profile image
Paul Cortes

How could put his in internet?? Deploy in a server?