Spring Boot Tutorial for Beginners

1- What is Spring Boot?

Spring Boot is a Project lying on  the IO Execution layer of Spring IO Framework.

Spring Boot is the next step of Spring to make Spring easier in setting up and developing applications. For Spring Boot, Spring configuration are minimized. Spring Boot supports embedded containers in allowing web applications to be able to run independently without deploying on Web Server.

You can use spring boot to create Java Web application running via command line 'java -jar' or export to war file to deploy on Web Server as usual. Spring Boot supply you a "CLI Tool" to run spring scripts.
Spring Boot can be explained simply by the illustration below:

Below is the benefits of Spring Boot:

  1. It is very easy to develop Spring Based applications with Java or Groovy.
  2. It reduces lots of development time and increases productivity.
  3. It avoids writing lots of boilerplate Code, Annotations and XML Configuration.
  4. It is very easy to integrate Spring Boot Application with its Spring Ecosystem like Spring JDBC, Spring ORM, Spring Data, Spring Security etc.
  5. It follows “Opinionated Defaults Configuration” Approach to reduce Developer effort
  6. It provides Embedded HTTP servers like Tomcat, Jetty etc. to develop and test our web applications very easily.
  7. It provides CLI (Command Line Interface) tool to develop and test Spring Boot(Java or Groovy) Applications from command prompt very easily and quickly.
  8. It provides lots of plugins to develop and test Spring Boot Applications very easily using Build Tools like Maven and Gradle
  9. It provides lots of plugins to work with embedded and in-memory Databases very easily

What do you need to start with Spring Boot?

Spring Boot is formed from the idea of simplifying the Spring Framework, all of Spring applications will be created by Spring Boot, and here is the beggining time, the new IDEs only start creating Spring Boot application. Spring guidelines still use the traditional way, and it takes time to change. Now, you need to how to create a traditional Spring application.

You can start with Spring Framework here:
Spring for beginners
Spring MVC tutorial for Beginners

2- The objective of the document

In the post, I will create a Hello Spring Boot application, it is a simple Web application using Spring Boot, and run this application independently without deploying on Web Server.

Spring Tool Suite (STS)

To build Spring Boot applications on Eclipse IDE, you need to install Spring Tool Suite, this is an extended Plugin supporting Spring programming on Eclipse.

If you have not installed it, let's see the guideline here:
Here is the project image after being completed:
Previewing the application:

3- Create Spring Boot Project

On Eclipse, creating a project by using Spring Tool Suite (STS):
  • File/New/Other...
  • Name: HelloSpringBoot
  • Group: org.o7planning
  • Artifact: HelloSpringBoot
  • Package: org.o7plannng.hellospringboot
  • Group Id: org.o7planning
  • Artifact Id: HelloSpringBoot
  • Packing: war
Project has already been created, below is the default structure of the Project.

Build Project:

You need to build project and make sure that everything is perfect.

Do you receive error?

If you get error when running "Maven Install", let's see further handling in the appendix of this post:

4- Some explanation of Spring Boot

In the above step, you have just created a Spring Boot Project, let's see pom.xml content:

pom.xml

There are 3 key points in pom.xml (1),(2), (3) as shown below, Spring Boot supports you in simplifing the declaration of Spring libraries.

spring-boot-starter-parent

spring-boot-starter-parent is a available project in Spring Boot. The dependent libraries is declared in spring-boot-starter-parent, your project only inherit it. You only need to declare <parent> in file pom.xml of your project.
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <!-- lookup parent from repository -->
    <relativePath/>
</parent>
You can overwrite the default dependence of Spring Boot. The topic will be cleared here:
  • TODO

spring-boot-starter-web

Other “Starters” simply provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we will add a spring-boot-starter-web dependency
<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>

spring-boot-maven-plugin

Spring-boot-maven-plugin is the plugin providing necessary libraries that helps your project to be able to run directly without deploying on a Web Server. It helps to create a jar file that can be executable.
<plugins>

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

        <!-- ... -->

</plugins>

5- Running Spring Boot project

In the step above, you have created HelloSpringBoot project by Eclipse & Spring Tool Suite and there is no any changes. All things are created by default. Now you need some steps to run the application.
Your HelloSpringBoot application is running, you can try this link:
An error message "404 Not found" will appear when you run this link, but there are no any serious problems because your project has not any pages yet.
Creating hello.html file in the webapp folder:
hello.html

<h1>Hello Spring Boot!</h1>
And re-run the application with the link:

Run Configuration...

You can change the parameters for Server such as port, contextPath,... These problems are mentioned in the appendix at the end of the post.

6- How Spring Boot run?

When you create a "Spring Boot Web Appp", there are 2 classes generated by the tool "Spring Tool Suite":
  • ServletInitializer

  • HelloSpringBootApplication

Your application is started by executing class HelloSpringBootApplication. The class is annotated by @SpringBootApplication.
HelloSpringBootApplication.java
package org.o7planning.hellospringboot;

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

@SpringBootApplication
public class HelloSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloSpringBootApplication.class, args);
    }
   
}
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes:
Therefore, @SpringBootApplication supports you in automatically configuring Spring, and automatically scanning entire project in order to find out Spring components (Controller, Bean, Service,...)
ServletInitializer class helps to initialize the Web application, to register web resources with the Spring application.
ServletInitializer.java
package org.o7planning.hellospringboot;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(HelloSpringBootApplication.class);
    }

}

7- Spring MVC with Spring Boot

The example below uses JSTL, therefore, you need to declare dependencies in pom.xml:
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <scope>provided</scope>
</dependency>
See full pom.xml:
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>org.o7planning</groupId>
    <artifactId>HelloSpringBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>HelloSpringBoot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
       
        <!-- To use JSTL -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
       
        <!-- To use JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

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


</project>

 
You need to configure  ViewResolver.
WebMvcConfig.java
package org.o7planning.hellospringboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }

}
HelloWorldController.java
package org.o7planning.hellospringboot.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public String hello(Model model) {
       
        model.addAttribute("greeting", "Hello Spring Boot MVC");
       
        return "helloworld";
       
    }

}
/WEB-INF/pages/helloworld.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Strig Boot Spring MVC - HelloWorld</title>
</head>


<body>
    <h1>${greeting}</h1>
</body>


</html>
Re-running the Spring Boot application, and assessing to the following address:

8- Appendix: Config Server

You are running the Spring Boot application above, you can change the server configurations such as Port, contextPath.....
Changing Server HTTP Port:
You can change other properties if you want, Eclipse will suggest the changeble properties and their meaning.

9- Appendix: Fix error

When you create a new Spring Boot project successfully, and you have no any changes but project notifes the error message of lacking library.
You can see the cause of error on Problems tab.
Description    Resource    Path    Location    Type
The container 'Maven Dependencies' references non existing library
'D:\DevPrograms\Repository\org\springframework\boot\spring-boot-autoconfigure\1.4.1.RELEASE\spring-boot-autoconfigure-1.4.1.RELEASE.jar'   
HelloSpringBoot        Build path    Build Path Problem
The cause here can be that the jar files have been downloaded from Maven Repository to the local machine was broken, and can not be read by the application. You can need to delete them on the local folders so that Eclipse can be re-download the jar files.
Deleting the folder:
  • D:\DevPrograms\Repository\org\springframework\boot\spring-boot-autoconfigure