Java Programming Examples

Spring Boot Hello World Example

 
In this section we will show you 5 steps to create a spring boot application to display a hello world example page.

Technologies used:

  • Spring Boot 2.7.1.RELEASE
  • Java 17
  • Maven 4.0.0
  • STS IDE

1. Create New Project

2. Project dependencies

when you finish it successfully. you will get pom.xml file as below:

pom.xml

<?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.7.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.gardenworktoday</groupId>
	<artifactId>spring-boot-hello-world-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-hello-world-example</name>
	<description>Spring Boot Hello World Example</description>
	<properties>
		<java.version>17</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-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>




3. The controller

HelloWorldController.java

package com.gardenworktoday.demo;

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

@Controller
public class HelloWorldController {
	
	@GetMapping({"/","/helloworld"})
	public String helloWorld() {
		return "hello_world";
	}

}

 

hello_world.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring Boot Hello World</title>
</head>


<body>

<h1>Hello World</h1>

</body>
</html>

4. Project directory

 

5. View on Browser

Open the browser and invoke the URL http://localhost:8080/helloworld. we can see the hello world that compile from controller.

Leave a Reply

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

Close
Close