Spring Boot Hello World Example In STS

Spring boot is used to help developer to develop spring application quickly and easily. It can help you in following aspects.

  1. Create spring project file structure and related source files with wizard.
  2. Get and add correct dependencies jars into your spring project automatically from 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 spring tool suite ( STS ) or Spring Initializr.

This article will show you example about how to create spring boot project with 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.
    spring tool suite file new spring starter project
  3. Input related information in the spring starter project popup dialog as below. Please note if you do not want to use default location to store the project files, you need to browse to your custom folder.
    spring tool suite spring starter project detail info
  4. Click Next button in above dialog to go to Spring Starter Project Dependencies dialog. In this dialog, you can select which module you want to use in this project. As hello world example, we just select Web and Thymeleaf module, then spring boot will download required jars for Web and Thymeleaf module into this project later.
    select web and thymeleaf module in spring tool suite
  5. Click 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 have been added into the project. All this jars are downloaded from spring central repository.
    spring boot auto added web and thymeleaf jar files and auto generated source files
  6. From above spring boot added jars you can see that tomcat embed jar is also there, this means you can start this application with an embed tomcat as web server.
READ :   iOS Swift Hello World Example

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 user manually. It is a spring mvc controller file which will receive client request and dispatch related web page to client.
  3. helloWorldPage.html : Because we use Thymeleaf module, we create this file to display the hello world to client. This file is located in src/main/resources/templates folder.

2.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

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

<!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.
    run as spring boot app to run spring boot application in spring tool suite
  3. Then you can see embed tomcat server started log information output in 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.

To execute the hello world example in command line we need to first Package Spring Boot Hello World Project Into Jar file and then Run It In Command Line.

  1. Right click the pom.xml file in the project file list panel.
  2. Click Run As —> Maven build menu item.
  3. Input package in the Goals input text box.
    sts maven build configuration dialog goals package
  4. Click Run button. When build process complete, right click target folder in left project file list panel and click Refresh menu item in popup menu list. You can find the generated HelloWorld-0.0.1-SNAPSHOT.jar file.
  5. 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.
READ :   How To Read External Properties File In Spring Using PropertyPlaceholderConfigurer

5. Create Spring Boot Project With Spring Initializr.

This is a web version tool which can help you to create spring boot project structure.

  1. Open a web browser and go to https://start.spring.io/.
  2. Click switch to full version link to open full element selection page.
    spring initializr switch to full version link
  3. Input related information such as group, artifact and select required dependencies module.
  4. Click Generate Project button. Then a zip file will be downloaded.
  5. Unzip the file and you can see the spring boot file structure inside it.
(Visited 1,037 times, 1 visits today)

Leave a Reply

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.