Simple Spring Boot Example

By | 2018-08-04T15:42:53+00:00 February 25th, 2017|Spring Boot|3 Comments

In this tutorial, I am going to show you the most popular and trending module in Spring Framework that is Spring Boot. By this tutorials you can get to know how to write a Simple Spring Boot Example.

Used Technologies :

  • Spring 3.2.3
  • Spring Boot 1.5.1
  • Java 8
  • Maven 3

Spring Boot Example :

Here I am going to implement a basic hello world spring boot example.

Project Structure :

Spring Boot Example

Maven Dependencies :

To make our example as simple as possible, I have placed dependencies in pom.xml what just I want.

pom.xml
<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>org.springframework.samples</groupId>
<artifactId>Spring_Boot_Example</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>

<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>

</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.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>

Create Application Class :

Application.java
package com.onlinetutorialspoint.spring.boot;

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

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

@SpringBootApplication annotation tells the spring application context, it is an spring boot application. Most of the developers can used to define the spring boot main classes with the @Configuration, @EnableAutoConfiguration and @ComponentScan annotations. Since these annotations are mandatory to every Spring application, the Spring Boot given us an annotation called @SpringBootApplication instead.

Here @Configuration + @EnableAutoConfiguration + @ComponentScan = @SpringBootApplication

Create a Controller Class :

HelloController.java
package com.onlinetutorialspoint.spring.boot;

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

@RestController
public class HelloController {
    @RequestMapping("/")
    public String index() {
        return "Hello World Spring !";
    }
}

Running the Spring Boot Example :

On e of the biggest advantage of Spring boot application is, to run we don’t deploy the application in any server. We can run the above spring boot example as a simple Java standalone application.

Run the main method in Application.java.

If every thing goes well, you can find the below text in your console.

Spring Boot Example out
Run it :

Spring boot example output

Happy Learning 🙂

Download Example

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

3 Comments

  1. preethishetty678@gmail.com'
    Spring Boot Training March 2, 2018 at 9:36 am - Reply

    Looking nice information about Spring Boot
    Plz do keep sharing on

  2. bhanuprat12345@gmail.com'
    bhanu pratap July 24, 2018 at 2:57 pm - Reply

    Good Article,valuable information, Best software Training institute in Bangalore

  3. vinodkumar.mvn@gmail.com'
    vinod kumar mvn August 31, 2018 at 2:45 pm - Reply

    The Article is very Informative and Worth Reading For the Candidates like me. Currently, I am learning spring boot training and this Article is helpful for me to understand the things very easily.

    Thanks

Leave A Comment