Building an Application with Spring Boot

In this post, we will explore Building an Application with Spring Boot. We will cover different aspects of Spring Boot along with different options to build an application using Spring Boot.

1. Introduction

Spring Boot is an opinionated, convention over configuration. Spring Boot takes away most part of the project set up by taking an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need. Spring Boot makes it easy to create a Spring-powered enterprise application with minimum fuss.

2. Spring Boot Features

Spring Boot provides the following feature out of the box

It simplifies Spring dependencies by taking the opinionated view (we will discuss it in more details). Spring Boot provides a pre-configured set of technologies/framework to reduces error-prone configuration so we as a developer focused on building our business logic rather than thinking of project setup. You really don’t need those big XML configurations for your project. Embed Tomcat, Jetty or Undertow directly. Provide opinionated Maven POM to simplify your configuration

3. Creating Spring Boot Project

One of the main challenges to starting up a new project is the initial setup for the project. We need to take a call about the different directory structure and also need to make sure we are following all the industry standards. If you are using Maven, you might already use Maven startup artefact which helps us to do those initial setups more quickly.

Spring Initializr is another great tool to quickly start Spring Boot projects. Spring Initializr is a web application that generates Spring Boot projects. Keep in mind it will only generate project structure and not any code for you based on your preference ( Maven or Gradle ). If you are starting your project, my recommendation is to start with Spring Initializr.

There are multiple ways to use Spring Boot Initializr to generate project structure for you.

Using Spring Initializr Web Interface. Use Spring Boot CLI tool. Use your IDE

3.1 Using Spring Initializer Web Interface

This is the simplest way to generate project structure for your application.Open Spring Initializr Web interface your browser and you will be presented with a wizard to start your configurations.

It requires you to fill information in the web interface to start with

What kind of project you want to generate (Maven or Gradle) What is your preferred language (Apart from Java you will get an option for Kotlin and Groovy) Spring Boot Version Standard project group and artefact details. Dependencies.

Dependencies is an interesting feature in the web interface, based on your selected Dependencies, we interface will automatically add Spring Boot Starter dependencies in the generated pom.xml file. In case you want a more control on the generated project structure or not sure what all dependencies you want to add to your project, click on the “Switch to the full version”.

With the full version, you have the option to select Java version, packaging mode (maybe .war for traditional deployment) along with an option to select dependencies for your project. Once you click on “Generate Project” button, Spring Initializr will generate a project and it will give you a zip to download. You can import the unzipped project as a simple Maven/ Gradle based project in your IDE.I will not be covering details how you can import this in your IDE. Please refer to relevant IDE document for more details.

3.2 Using Spring Boot CLI

We can also use Spring Boot CLI to generate the structure for your project, once you have installed CLI, open the command prompt and type spring. If CLI is installed correctly, you should be seeing the very similar output on typing spring.

localhost:~ javadevjournal$ spring usage: spring [--help] [--version] [<args>] Commands are: run [options] [--] [args] Run a spring groovy script

We can use init as an additional parameter with the spring to create a new project. Spring Boot CLI will internally go to use the start.spring.io to generate project structure for you.

localhost:~ javadevjournal$ spring init --dependencies=web springboot-demo-project Using service at https://start.spring.io Project extracted to '/Users/umesh/springboot-demo-project'

It created springboot-demo-project directory with a maven based project using spring-boot-starter-web. This will create a project with the same default setting as available on the start.spring.io web interface. We can pass different parameters to customize project generation.

Let’s say we want to generate our project based on Java 1.7 , we can pass --java-version=1.8 as an additional parameter to Spring Boot CLI.

spring init --java-version=1.7 --dependencies=web springboot-demo-project

When you will run above command, it will automatically set java-version in the generated pom.xml file as 1.7.

<properties> <java.version>1.7</java.version> </properties>

If you are not sure what are the capabilities of the Spring init service, run init command with --list flag .

spring init --list

4. Peek Inside pom.xml

Let’s look into pom.xml file to understand Spring Boot configurations in more detail. I will cover only Spring Boot related changes in pom.xml . Here is the pom.xml file from our sample project.

<?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.umeshawasthi</groupId> <artifactId>ems</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>ems</name> <description>Employee Management System outline Spring Boot Features</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- project properties repository --> <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> <!-- Spring boot test depedency --> </dependencies> </project>

One of the main features of Spring Boot is the Starters, they are an easy way to add required dependencies (jars) in your classpath. When using Spring Boot, we don’t have to add jar/dependencies in your classpath (In case a starter is not available, you might have to add these dependencies in the pom.xml or can create your own custom starter). We need to add correct “Starters” in our pom.xml file and Spring Boot will add those dependencies automatically.

5. Application Entry Point

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

5.1 @SpringBootApplication Annotation

Our main class is using @SpringBootApplication annotation. @SpringBootApplication is equivalent to using @Configuration , @EnableAutoConfiguration and @ComponentScan with their default values.If you are starting your project, it’s recommended to use annotation. Using @SpringBootApplication in your main class is equivalent to following 3 annotations

@Configuration as a source of bean definitions. @EnableAutoConfiguration It gives Spring Boot an idea how you want to configure your application. @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

5.2 Main Method

Another interesting feature of our main class is the main method. This is a standard method that will follow standard Java workflow. Our main class will pass on control to Spring Boot SpringApplication class. SpringApplication Class run method will be used to the BootStrap application. We will take a more deep look into the SpringApplication later section.

6. Hello World Controller

package com.javadevjournal.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/") public String helloWorld(){ return "Hello World!!"; } }

There is nothing special in our Controller . It’s a standard Spring-MVC controller with standard Spring MVC annotations.

6. Running Application

It’s time to run our first Spring Boot powered application. We have multiple ways to run our Spring Boot application.

If you are using IDE, you can use IDE to run your application. We can use mvn spring-boot:run command from the root directory to start our first Spring Boot application.

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.4.RELEASE) 2017-07-08 15:49:50.319 INFO 1238 --- [ main] com.javadevjournal.demo.EmsApplication : Starting EmsApplication on localhost with

Open the browser of your choice and type http://localhost:8080 , see “Hello World” as an output.

Summary

Spring Boot provides a good boost to the Spring-based applications. In this post, we learned about different options of Building an Application with Spring Boot. Setting up a new project is always a challenging task and we need to manage all dependencies but with Spring Boot, it was easy and we able to run our first web application with only new lines of code without thinking much about the required dependencies or the deployment.