Spring Boot - Hello world example



Spring Boot - Hello world example

Spring boot is framework that is used for creating stand alone, production-grade Spring based applications with minimal effort. It provides a convenient way to bootstrap a Spring application that will be started from a main() method.

In this post, we will learn how to create a Spring Boot application which prints "Hello Spring Boot".

Tools and technologies used for this application are-

  • Spring Boot 1.5.4.RELEASE
  • JavaSE 1.8
  • Maven 3.3.9
  • Eclipse Neon.3

Let’s begin step by step...

 

Step 1 - Maven project

Create a maven project for our Spring Boot application in Eclipse IDE.

You can refer this article to learn - How to create a maven project in eclipse IDE.

 

Step 2 - Jar dependencies

Open your pom.xml file and write the following code in it.

<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.boraji.tutorial.springboot</groupId>
  <artifactId>spring-boot-hello-world-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </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.

The spring-boot-starter-parent provides the common configuration such default java compiler level, plugin configuration, UTF-8 source encoding, dependency management etc.

There are various starters such as spring-boot-starter-web for building Spring web application, spring-boot-starter-web-services for  building Spring web service application.

 

Step 3 - Controller class

Create a simple rest controller class inside com.boraji.tutorial.springboot.controller package as follows.

HelloWorldController.java

package com.boraji.tutorial.springboot.controller;

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

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

 

Step 4 - Main class

To bootstrap spring boot application, create a main class annotated with @SpringBootApplication annotation.

MainApp.java

package com.boraji.tutorial.springboot;

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

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

Then @SpringBootApplication annotation 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.

 

Step 5 - Project structure

Review the final spring boot project structure.

Spring Boot - Hello world example | BORAJI.COM

 

Step 6 - Run application

Run the MainApp.java class as Java application or you can use the mvn spring-boot:run command.

After executing the main class, the console output will look like as follow.

Spring Boot - Hello world example | BORAJI.COM

Now, enter the http://localhost:8080 in browser's address bar and see the output.

Spring Boot - Hello world example | BORAJI.COM

 

Step 7 - Packaging Spring Boot application as an executable jar

To create an executable jar file you need to add the spring-boot-maven-plugin to your pom.xml.

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

After adding the spring-boot-maven-plugin to your pom.xml, you can use the following maven command to package your spring boot application as jar.

mvn clean package

You can find the generated jar file in the target directory as follows

Spring Boot - Hello world example | BORAJI.COM

 

Step 8 - Running executable jar from command line

Use the following the following command to run the jar from the CMD.

java -jar spring-boot-hello-world-example-0.0.1-SNAPSHOT.jar

After running the above command in CMD, the console output will look like as follows.

Spring Boot - Hello world example | BORAJI.COM