Spring Boot Hello World Example

Spring Boot Hello World Example – Spring boot is a framework developed in java. This spring boot application example project shows hello world example using Spring Tool Suite ( STS IDE ). This spring boot starter project example is explained step by step.Finally, in this tutorial, we will learn how to run the hello world example project in spring boot using Spring Tool Suite.

In this page, we’ll help you how to start the Hello World application in Spring Boot using Spring Tool Suite. To develop this application, we will use JDK 8 and Maven. Here we will explain step by step procedure with images how to create this application, how to compile and how to run the application in order to see the message “Hello World” in the console. Start Spring Boot Hello World Project



Start Spring Boot Hello World Project

Click on the “File “menu in the top left corner-> then click on the “New “menu-> then click on the “Spring Starter Project “button in the image below.

The link above will open a wizard “New Spring Starter Project” as a pop-up window. Change the Name to “SpringHelloWorld”. By default, the location will show in the work space. If you want to change to another location, un-check the “Use default location” location and select the location you want. Change Group, Artifact, Description, Package as shown in the picture. Leave the remaining field as the default value. To move next page, click on the “Next “button.

The window “New Spring Starter Project Dependencies “will open on the next page. In various articles, we will see the project dependencies. For now, keep all default values in all fields. To move next page, click on the “Next “button.

It will show “Site Info” on the next page as shown below. To create a default project, click “Finish” button.



Default Spring Boot Project File Structures

Now it creates the default Spring Boot Project. The directory structures are created as shown in the image below. The file “SpringHelloWorldApplication.java” was created in the source folder. This is the main method starting file. A default configuration file called “application.properties” is created in the resource folder. A default test file with default code is created in the test folder “SpringHelloWorldApplicationTest.java”. In the root directory, a pom.xml file is created.

The file pom.xml will contain the code below. By default, spring-boot-starter and spring-boot-starter-test maven dependency is added in the pom.xml file. spring-boot-starter dependency will download all the spring framework core jar files. The framework for dependency injection ( DI ) and inversion of control ( IOC ) is added from this spring-boot-starter maven dependency.

<?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 http://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.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.yawintutor</groupId>
	<artifactId>SpringHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringHelloWorld</name>
	<description>Spring Boot Hello World Project</description>

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>

The main spring boot file “SpringHelloWorldApplication.java” is displayed as below. It contains a main method by default. The annotation @SpringBootApplication is added in this class. This annotation informs about starting of the spring boot application. The main method is the starting point of the application. SpringApplication.run() api starts the project and the command line arguments are passed into the application.

package com.yawintutor.helloworld;

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

@SpringBootApplication
public class SpringHelloWorldApplication {

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

}

In the test folder, the default test java file “SpringHelloWorldApplication.java” contains the default test code. The annotation @SpringBootTest is used to inform about the unit test classes. The annotation @RunWith loads the Junit framework using the starting class SpringRunning.class.

package com.yawintutor.helloworld;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringHelloWorldApplicationTests {

	@Test
	public void contextLoads() {
	}

}


Running the default Spring Boot Application

Open the file “SpringHelloWorldApplication.java” in the Project Explorer window by double-clicking on the file name.

You can use the Spring Tool Suite to run the Spring boot application in three ways.

  • First option – Click on the “Run “menu in the menu-> Click on “Run As “-> click on “2. Spring Boot App”
  • Second option – Right click on “SpringHelloWorldApplication.java” -> Click on “Run As” -> click on “Run Spring Boot App”
  • Third option- Click in the toolbar on the “Run” button.

The output is displayed in the console window as below. By default, the default log statements are displayed in the console window.



Create Hello World Application

Open the file “SpringHelloWorldApplication.java” in the Project Explorer window by double clicking on the file name. Add the code as below. The system.out.println contains a string “Welcome to Hello World” that is printed in the spring boot console.

package com.yawintutor.helloworld;

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

@SpringBootApplication
public class SpringHelloWorldApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringHelloWorldApplication.class, args);
		System.out.println("Welcome to Hello World!");
	}

}

Run the application again, Now “Hello World” will be printed in the console window. The output console window will appear as below The console window starts with a spring banner, followed by default spring boot loading log files. In the last, the “Welcome to Hello World” message is printed in the spring boot console window.

On this page, we explained how to start with Hello World example in Spring Boot using the Spring Tool Suite.



Related Articles

Leave a Reply

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