Hello, Spring Boot

Nice thing about Spring Boot applications is that, they are just standard Java applications. So we don’t need anything special to run them or debug them.

** Spring Boot gives us an entry point so you know exactly where the application starts and what’s going on. There is a main Application class with a static void main method.

Spring Boot gives you Java application to use with your own apps via an embedded server. It uses Tomcat, so you do not have to use Java EE containers.

Step 1: Go to http://start.spring.io

Spring Initializr is a web based tool that allows you to generate a template project and automatically set all the required dependencies according to the selected technologies.

Create a Spring boot project like this :

springboot2

This will generate a project zip file (HelloSpringBoot.zip). Download the file.
Unzip the file to a folder.

Step 2 : Import the project in Eclipse.

springboot3

Step 3 : This will import the project and Maven will pull in all the required dependencies.

pom.xml include these :

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>2.0.0.BUILD-SNAPSHOT</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent>

<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> 
     </dependency> 
</dependencies>

Step 4 : Just run the application. Since, we have chosen web dependency, this will start embedded Tomcat server when we run the application.

springboot4

Step 5 : Create a simple class and annotate it with @RestController. Write a simple REST API that will return a string and have RequestMapping set to “/hello”

springboot5

package com.rdayala.springboot.basics.HelloSpringBoot;

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

@RestController
public class HelloSpringBoot { 
    @RequestMapping("/hello") 
    public String home() { 
        return "Hello, Spring Boot!!"; 
    }
}

Step 6 : Run the Spring boot application. This will start embedded Tomcat server and will bind the request mappings.

Step 7 : You can see the application working by accessing the REST API.

springboot6

Spring Boot Devtools : 

Whenever you make any changes and want them to be automatically reflected without restarting the server, you need to add spring-boot-devtools dependency to your project.

<dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-devtools</artifactId> 
</dependency>