Home » Spring Boot » Spring Boot - Hello World Example

Spring Boot - Hello World Example

Simple Spring Boot Web Application

In this tutorial, we will discuss how to create a very simple spring boot web application using maven which will print "Hello World".

Spring Boot lets you create an application with minimal configurations. When creating a simple spring boot web application, spring boot eliminates the need of creating a web.xml, spring configuration file or other xml configurations. Spring Boot automatically create those configurations for you just by adding the right dependency in your maven or gradle project.

Please follow this article carefully, as this is the first spring boot application I am going to explain each and every step with screenshot. In the next tutorial we will discuss about SPRING INITIALIZR which is a cool feature provided by spring.io to bootstrap Spring Boot Project. We are going to use SPRING INITIALIZR extensively for all examples. But for better understanding of Spring Boot configuration this tutorial is very important.

Tools and Technologies used for this application :

  • STS
  • Spring Boot 2.1.3.RELEASE
  • Java 1.8
  • Maven

Let's Start development of the project step by step. First we need to create Spring Starter Project in STS. Follow the steps mentioned below to create the Project.

Step 1 : Create Spring Starter Project

The very first step isto create spring starter project. So Open STS (Spring Tool Suite) then click on File -> New -> Spring Starter Project as shown below.

Spring Boot Hello World


Step 2 : Provide basic details of the project

Next screen that will show up is given below.

Spring Boot Hello World

Provide Name, Group , Artifact ,Description and Package name and click on Finish button. Here 'Artifact is name of the project and 'Group ' is the name of the base package.

Spring Boot Hello World


I am going to use following entry for this example.

Name : SpringBootHelloWorld
Group : com.jwt.spring.boot.demo
Artifact : SpringBootHelloWorld
Package :com.jwt.spring.boot.demo

Leave following default value as it is :

  • Service Url : https://start.spring.io
  • Type: Maven( You need to change it to Gradle if you prefer Gradle as build tool)
  • Packaging: Jar(Can be changed to War as per requirement)
  • Java Version: 8(Can be changed to 11 as well as of writing this tutorial.)

Spring Boot Hello World


Step 3 : Select Dependencies of Project

Click Next and we will see the following screen. At the time of writing this tutorial, the Latest release version of Spring Boot was 2.1.4, which is selected by default, so leave it as is. After that add dependencies as per requirement. For a simple web application web dependecy is sufficient. Just select web dependecy as shown below.

Spring Boot Hello World


Step 4 :

Now Click on Finish. As you can see in the following screenshot, a Maven project with name SpringBootHelloWorld is added in the STS :

Spring Boot Hello World


Let us expand Maven Dependencies of this project and you can see Spring Boot has added so many jar files which is reuired for the application.You can see all of these dependencies in below screen shot.

Spring Boot Hello World


Before the introduction of Spring Boot, developer has to add all these dependencies manually and considering compatibility between different jar versions. It was really time consuming process, but if you are using spring boot then no need to worry about it. Spring Boot takes care of all necessary dependencies. We just need to instruct Spring Boot only at a high level that which kind of dependencies we want to add, just like in this case we asked spring boot to add Web dependencies and Spring Boot added all web related dependencies along with other core dependencies.

Open pom.xml file of the project. pom.xml file is given below.

<?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.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.jwt.spring.boot.demo</groupId>
	<artifactId>SpringBootHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootHelloWorld</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-web</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>

Spring Boot provides the various "Starters" for building Spring Boot based java application. The spring-boot-starter-parent is a special type of starter, which is used as a parent in pom.xml file of any kind of Spring Boot application.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>

All Spring Boot projects typically use spring-boot-starter-parent as the parent in pom.xml.In most of the cases, your maven project POM will simply inherit from the spring-boot-starter-parent project. The spring-boot-starter-parent will

  • provides useful Maven defaults.
  • provides dependency-management section so that you can omit version tags for dependencies you need for your own project.
  • The spring-boot-starter-parent provides the common configuration such default java compiler level, plugin configuration, UTF-8 source encoding, dependency management etc.

spring-boot-starter-web dependency is added additionally as we selected Web dependency while creating project. Which is shown below.

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

On adding spring-boot-starter-web, we can see that lots of additional dependencies which are required for web project are added to the build path.You can see all the dependecies by clicking on web dependecy in pom.xml. This includes embedded tomcat dependencies as well, such that we need not install and configure Tomcat separately to deploy Spring Boot application:

To bootstrap spring boot application Spring Boot need one main class in the application, which acts as a starting point for the Spring Boot Application. If you are initializing project using spring initializer or STS, by default this class will be created for your application.

package com.jwt.spring.boot.demo;

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

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

@SpringBootApplication initializes Spring(Component Scan) and Spring Boot (Auto Configuration). @SpringBootApplication indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

From the main method of this class, SpringApplicaiton class’s run method is called. This method makes sure that Spring application’s applicationContext(the Spring Container) is initialized.

Step 5: Create Controller Class

To be able to handle web request, our application should have controllers. Here is a sample controller HelloWorldController that handles the request to the path "/hello". Create a simple rest controller class inside "com.jwt.spring.boot.demo.controller" package as shown below.

package com.jwt.spring.boot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
	@GetMapping("/hello")
	public String sayHello() {
		return "Hello World Spring Boot!!";
	}
}

Step 6: Running the Application

Run the main class SpringBootHelloWorldApplication and you will notice that jar is automatically deployed to embedded Tomcat server and Tomcat server has been started at port 8080.

Check Console log of eclipse:

Spring Boot Hello World


Now launch browser and enter the http://localhost:8080/hello in address bar. You can see below output in browser.

Spring Boot Hello World


Thats it, in this post we saw that How we can Create a Spring Boot Project in sts tool and how we can deploy and run it on embedded Tomcat server.

In the next article I will discuss about how to configure Spring Boot Development Environment Setup.

You can get the whole code used in this post from our GitHub repository:

GitHub

Previous Next Article