HomeSpring BootSpring Boot Hello World Example

Spring Boot Hello World Example

This is very basic Spring Boot Hello World REST application with very minimal configuration, created using spring-starter-project in Spring Tool Suite IDE , Spring Boot 2 and Maven.

1. Spring boot demo example

1.1. Technologies used :

  1. Spring Boot 2.2.2.RELEASE
  2. Spring 5.2.2.RELEASE
  3. Maven 3
  4. Java 8
  5. Spring Tool Suite 3.9.8.RELEASE

1.2. Project Structure :

Spring Boot Hello World

2. Project Dependencies :

2.1. 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.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javabydeveloper</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>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

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

</project>

3. @SpringBootApplication

Many Spring Boot developers use @Configuration, @EnableAutoConfiguration and @ComponentScan together for the auto-configuration. A single @SpringBootApplication annotation can be used to enable those three features.

package com.javabydeveloper.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);
	}

}

4. A Simple Rest controller

Following is a simple Rest api controller which servers GET request for the /hello mapping.

package com.javabydeveloper.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
	
	@GetMapping("hello")
	public String sayHello() {
		return "Hello Developer, you are awesome!";
	}

}

5. application.properties

By default Spring Boot application uses 8080 server port and root application directory name as context path. To customize these values add following properties and values accordingly in in application.properties file.

server.port=8181
server.servlet.context-path=/helloworld

6. Start Spring Boot Application

Run as Spring Boot application in your IDE or using maven mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.2.RELEASE)

2019-12-15 00:56:49.095  INFO 8296 --- [           main] c.j.d.SpringBootHelloWorldApplication    : Starting SpringBootHelloWorldApplication on ADMINA565 with PID 8296 (Z:\D\Workspaces\SpringBoot\SpringBootHelloWorld\target\classes started by admin in Z:\D\Workspaces\SpringBoot\SpringBootHelloWorld)
2019-12-15 00:56:49.099  INFO 8296 --- [           main] c.j.d.SpringBootHelloWorldApplication    : No active profile set, falling back to default profiles: default
2019-12-15 00:56:51.175  INFO 8296 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8181 (http)
2019-12-15 00:56:51.190  INFO 8296 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-12-15 00:56:51.190  INFO 8296 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-15 00:56:51.381  INFO 8296 --- [           main] o.a.c.c.C.[.[localhost].[/helloworld]    : Initializing Spring embedded WebApplicationContext
2019-12-15 00:56:51.381  INFO 8296 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2074 ms
2019-12-15 00:56:51.687  INFO 8296 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-15 00:56:51.956  INFO 8296 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8181 (http) with context path '/helloworld'
2019-12-15 00:56:51.961  INFO 8296 --- [           main] c.j.d.SpringBootHelloWorldApplication    : Started SpringBootHelloWorldApplication in 3.765 seconds (JVM running for 6.03)

7. Test the Hello World Application

7.1. Access /hello GET request using http://localhost:8181/helloworld/hello. Upon successful request you see the following results in your browser.

Spring Boot Hello World Test

In this article we covered very basic Spring Boot Hello World REST application with very minimal configuration, created using spring-starter-project in Spring Tool Suite IDE , Spring Boot 2 and Maven.

Download source code

Download Spring Boot Hello World example from our Github.
SpringBootHelloWorld

References

  1. Start Spring Boot Application
  2. @SpringBootApplication
  3. Change the Application Port
  4. Spring @ComponentScan

LEAVE A REPLY

Please enter your comment!
Please enter your name here