Spring Boot Hello World Example In STS

Spring boot is used to help developers develop spring applications quickly and easily. It can help you in the following aspects.

  1. Create spring project file structure and related source files with a wizard.
  2. Get and add correct dependencies jars into your spring project automatically from the spring repository.
  3. You do not need to care about the dependency jar file’s name, version, and compatibility. All this is done by spring boot automatically. You just need to tell spring boot which module you need such as web, security, and JPA, etc.
  4. You also do not need to configure spring beans by yourself, all the configuration will be done by spring boot automatically also.
  5. You can create the spring boot project with the Spring Tool Suite ( STS ) or Spring Initializr.

This article will show you examples of how to create a spring boot project with the Spring Tool Suite and Spring Initializr.

1. Create Spring Boot Project With STS.

  1. Download spring tool suite from link https://spring.io/tools.
  2. Open Spring Tool Suite, click File —> New —> Spring Starter Project menu item on the top menu bar.
  3. Input related information ( Name, Location, Type, Group, Package, etc) in the popup New Spring Starter Project dialog. Please note if you do not want to use the default location to store the project files, you need to browse to your custom folder.
  4. Click the Next button in the above dialog to go to the New Spring Starter Project Dependencies dialog. In this dialog, you can select which module you want to use in this project. As the hello world example, we just select the Web and Thymeleaf module, then spring boot will download the required jars for the Web and Thymeleaf module into this project later.
  5. Click the Finish to complete the wizard, now you can see the hello world project has been created in STS left panel. Expand the Maven Dependencies folder, you can see there are a lot of jar files that have been added to the project. All these jars are downloaded from the spring central repository.
  6. From the above spring boot added jars you can see that the tomcat embed jar is also there, this means you can start this application with an embed tomcat as a web server.

2. Spring Boot Hello World Project Source Files.

There are two java files and one Html file in this example.

  1. HelloWorldApplication.java: This file is created by the wizard automatically. You can run this class to start the embed tomcat server.
  2. HelloWorldController.java: This file is created by the user manually. It is a Spring MVC controller file that will receive client requests and dispatch related web pages to the client.
  3. helloWorldPage.html: Because we use the Thymeleaf module, we create this file to display the hello world to the client. This file is located in the src/main/resources/templates folder.

2.1 HelloWorldApplication.java

  1. HelloWorldApplication.java
    package com.dev2qa;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloWorldApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldApplication.class, args);
        }
    }

2.2 HelloWorldController.java

  1. HelloWorldController.java
    package com.dev2qa;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/")
    public class HelloWorldController {
    
        @RequestMapping(value="/helloWorld", method=RequestMethod.GET)
        public String helloWorld()
        {
            // The html file name is helloWorldPage.html.
            return "helloWorldPage";
        }
    }

2.3 helloWorldPage.html

  1. helloWorldPage.html
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <br/><br/>
    <center>Hello World!</center>
    </body>
    </html>

3. Execute Spring Boot Hello World Example In STS.

  1. Right-click the HelloWorldApplication.java file in Spring Tool Suite left panel.
  2. Click Run As —> Spring Boot App menu item in the popup menu list.
  3. Then you can see embed tomcat server started log information output in the log console.
  4. Open a web browser and input http://localhost:8080/helloWorld. It will display the hello world web page.

4. Execute Spring Boot Hello World Example In Command-Line.

  1. To execute the hello world example in the command line we need to first package the spring boot hello world project into a jar file and then run it in the command line.
  2. Right-click the pom.xml file in the project file list panel.
  3. Click Run As —> Maven build menu item to open the Edit configuration and launch dialog window.
  4. In the popup window, input package in the Main tab —> Goals input text box, input pom.xml in the Profiles input text box.
  5. Click the Run button. When the build process is complete, right-click the target folder in the left project file list panel and click the Refresh menu item in the popup menu list. You can find the generated HelloWorld-0.0.1-SNAPSHOT.jar file.
  6. CD to above jar file saved directory, and run java -jar HelloWorld-0.0.1-SNAPSHOT.jar in a terminal window. Then the embed tomcat server will be started.

5. Create Spring Boot Project With Spring Initializr.

  1. This is a web version tool that can help you to create a spring boot project structure.
  2. Open a web browser and go to https://start.spring.io/.
  3. Select project type ( Maven Project or Gradle Project ).
  4. Select language ( Java, Kotlin, Groovy ).
  5. Select Spring Boot version radio button.
  6. Input Project Metadata information such as Group, Artifact, Name, Description, Package name, Packaging ( Jar or War ), Java version.
  7. Click the ADD DEPENDENCIES button on the top right corner to add project needed libraries.
  8. Click the EXPLORE button on the page bottom to explore the spring boot project files in the web browser.
  9. Click the GENERATE button on the page bottom to create and download the spring boot project files in a zip file.
  10. Unzip the zip file and you can see the spring boot project file structure inside it.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.