1. What is Spring Boot
Spring Boot is a platform crafted to build mainly Spring-based applications. It was designed to create production-ready systems fast with minimum configuration and many things supported behind the scenes.
Some of the best Spring Boot features are:
- it can be run like any other java stand-alone application - no need to deploy WAR files,
- reduce development time - convention-over-configuration approach,
- can automatically configure Spring and 3rd party libraries,
- no more XML configuration hell, everything can be configured with annotations.
Spring Boot 2 requires from Java 8 to Java 11 to run. We recommend installing OpenJDK if you don't have it already on your OS. Additionally to build and deploy Spring Boot applications we will also need:
- maven 3.3+ (Install Apache Maven on Ubuntu) or
- gradl 4.4+.
3. Creating Your First Spring Boot Application
If we are sure that Java and Maven are installed on our operation system we can start developing our first Spring Boot application that will print 'Hello World!'.
To start, first we need to create a project using our favorite IDE or just creating a simple empty folder, and put
pom.xml file with the following content:
We will use maven to build our app so special folder structure is needed:
src/main/java- source directory,
src/main/resources- resources directory (we will put here configuration files like for example application.properties).
Spring Boot comes with many special libraries called 'Starters', that can be used to extend your application with new features. We alredy used base starter
spring-boot-starter-parent - it contains useful Maven defaults and provides dependency-management section. Since we are building a web application, we will also need to add
spring-boot-starter-web dependency to our pom.xml:
mvn dependency:tree command to see if our Spring Boot application is a fully MVC project with dependencies like Tomcat web server, Spring core library, logback and Spring Boot itself.
To finish our 'Hello World!' application we need to create a single Java class with
public static void main method that fires Spring Boot application.
Application class was described with the following annotations:
@RestController- is a stereotype annotation treat as a hint that this class will be accepting HTTP requests,
@EnableAutoConfiguration- that magic annotation makes Spring Boot automatically configure the application based on the dependencies in pom.xml.
@RequestMapping annotation set on index() method gives "routing" information to Spring Boot that HTTP requests with the
/ path will be from now handled with index() method.
4. Running Spring Boot Web Application
There are 3 methods to run Spring Boot application:
a) run from IDE
Use IDE to run Spring Boot application. For instance in InteliJ you just need to click green arrow button located near the Application class.
b) java -jar
To create executable jar file for Spring Boot application first we need to add a special maven plugin
spring-boot-maven-plugin in our pom.xml:
First build our project with
mvn clean install command, and after SUCCESS result, go to target folder. There should be
hello-world-0.0.1-SNAPSHOT.jar file there.
We can run Spring Boot from here using below command:
java -jar hello-world-0.0.1-SNAPSHOT.jar
b) mvn spring-boot:run
In project root folder type
mvn spring-boot:run command that should build your project and start Spring Boot right after that.
No matter what method we choose, if everything is correct and port 8080 is not occupied, there should be an output simillary to this one:
The last line tell us that server is up and running:
Started Application in 2.082 seconds (JVM running for 4.669)
Now we can go to our browser address bar and enter
http://locahost:8080 (8080 is a default Spring Boot port, if you want to change that port go here -> How to change Spring Boot port). There should be 'Hello World!' text.
You can download code used in this tutorial from GitHub: