Spring Boot Hello Starter App


This article show how to write a very simple Hello World Application in Spring. This application can be used as starting point for other applications.
We will go though the main components of the application, also code for the whole project is provided to download and play with it.

Technologies used

  • Java 11
  • Spring Boot 2.2.6.RELEASE
  • Maven 3.5.0
  • Eclipse IDE
This is a very simple Spring boot application with just one controller. The intention of the Application is to build a web app that when accessed will return a String message. This project is intended to help understand how to build a very simple Spring Application. So we are not touching other concepts like configuration, Profiles, etc.

Project Structure

Project code reference

The following paragraphs show important files for this sample project. Like the pom.xml file, HelloApplication.java (Spring Boot Application class) and the Controller class which is responsible for serving the request (HelloController.java)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <name>Spring Boot Hello</name>
 <description>Spring Boot Hello Simple Application</description>
  <relativePath />
Application Class
Spring Boot entry class that would be loaded and executed once the application boots up. It does not do much except that it has the annotation @SpringBootApplication to tell the spring boot framework that this is the entry point class. Also, it has @ComponentScan, so that Spring boot framework can scan and load any components from package com.bootng.
package com.bootng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloApplication {
  private static final Logger log = LoggerFactory.getLogger(HelloApplication.class);
  public static void main(String args[]) {
    log.info("about to call HelloApplication.run()");
    SpringApplication.run(HelloApplication.class, args);
    log.info("completed executing HelloApplication.run()");
Our only Controller class that is mapped to the path /hello and return a String as a response.
package com.bootng;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class HelloController {
  public  @ResponseBody String hello() {
    return "Hello Spring Boot!";

Run hello spring boot app

Build and Run Application
run the following command in the console to build the application and then run the application.
mvn clean install
mvn spring-boot:run
Console (Portion of the terminal logs)
INFO main c.b.HelloApplication:651 - No active profile set, falling back to default profiles: default 
INFO main o.s.b.w.e.t.TomcatWebServer:92 - Tomcat initialized with port(s): 8080 (HTTP)
INFO main o.a.c.h.Http11NioProtocol:173 - Initializing ProtocolHandler ["http-nio-8080"]
INFO main o.a.c.c.StandardService:173 - Starting service [Tomcat]
INFO main o.a.c.c.StandardEngine:173 - Starting Servlet engine: [Apache Tomcat/9.0.33]
INFO main o.s.b.w.e.t.TomcatWebServer:204 - Tomcat started on port(s): 8080 (http) with context path ''
Started HelloApplication in 1.384 seconds (JVM running for 1.716)
INFO main c.b.HelloApplication:18 - completed executing HelloApplication.run()
Access the application by
typing in the browser.
We should be able to see the page returning the message from the controller.
Download source code
  • git clone https://github.com/bootng/spring-boot-references
  • cd bootng-springboot-hello
  • mvn install
  • mvn spring-boot:run


    • This article shows how to build a sample web application from scratch.
    • This is a Maven-based project, similarly, we can have a Gradle based project also.
    • For the mvn install step, we need to be connected to the internet.
    • This is a simple web app that has only one request path mapped.
    • If we try with different paths then this App will show an error page.

    No comments :

    Post a Comment

    Please leave your message queries or suggetions.