Spring boot makes development process of application easier and less time consuming. It provides quick start to the application development with the help of starter dependencies which reduces time taken by the application developer for writing boiler plate codes.

In this post we will see how to create a simple spring boot application which prints “hello world”. In this post, we are using Maven build tool for developing our application along with Eclipse IDE.

Also we are using tomcat embed servlet container for running our application.

Following are the steps to create our first spring boot “hello world” application:

  • Create a maven project and modify the pom.xml content with following content.
  • Here, we are using spring-boot-starter-parent as parent project.
  • Adding spring-boot-starter-web dependency adds all required spring boot dependency jars into our application.

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.asb</groupId>
  <artifactId>SpringBootHelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <!-- Inherit defaults from Spring Boot -->
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.4.2.RELEASE</version>
    </parent>
    
  <!-- Use java version 1.7 -->
    <properties>
           <java.version>1.7</java.version>
    </properties>
        
    <!-- Add typical dependencies for a web application -->
    <dependencies>
     <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    </dependencies>
</project>
  • create a java class named as Application.java under com.asb package and add the following content. This class is the entry point of our spring application. This class contains a main() method, which is executed while starting our spring boot application.
  • @SpringBootApplication : This annotation adds provides our application with basic required configurations. By using this annotation, following annotations are internally  added : @Configuration, @EnableAutoConfiguration and @ComponentScan.
  • @Configuration : Makes the current class as source of bean definitions.
  • @EnableAutoConfiguration : Makes all beans available for application, based on class path settings, other beans and other property settings.
  • @ComponentScan : Spring application looks for classes, components, services, etc defined from this package level.
  • SpringApplication.run() will start our application once our application is run.

Application.java :

package com.asb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
         SpringApplication.run(Application.class, args);
    }
}
  • Create a controller class called MyController.java under package com.asb.controller as shown below.
  • @Controller : Makes the class a spring controller.
  • @RequestMapping : Used to map the requests into the defined request handler method. In this example, all the requests are handled by the method getMessage()
  • upon hitting the application(http://localhost:8080/), browser will display “hello world” string message on browser.

MyController.java :

package com.asb.controller;

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

@Controller
public class MyController {
    @RequestMapping("/")
    public String getMessage(){
        return "hello world";
    }
}
  • finally run the application by Right click on Application.java class > Run as > Java application.

hello_world_spring_boot

  • This will run our spring application. and console will display that application is up and running on port 8080 as shown below.

spring start

  • Following is the out put of our application displaying “hello world” upon hitting the application URL on browser(http://localhost:8080/).

spring-boot-hello-world example

Version Details:

  • Spring Boot 1.4.2.RELEASE
  • Eclipse Neon.2 Release (4.6.2)

Also read: