This is a quickstart introduction to Spring Boot that will teach you how to create your first Spring Boot application using just the Command Line.
From Java EE to Spring
The Spring framework was launched as an alternative framework to the Java Enterprise Edition. Instead of using components such as Enterprise JavaBeans with a complex protocol structure, Spring proposed a simpler approach focusing on dependency injection and aspect-oriented programming (AOP) to get Enterprise functionality of Java EE components but using simple Java objects (POJO).
The downside is that even though Spring is an easily extensible framework, starting from a minimal core, it is rather complex in terms of configuration.
In fact, most Spring libraries require an XML configuration as a buffer between the POJO classes and the actual component.The addition of third-party libraries also requires a specific XML configuration that can cause a strong development slowdown. Most Spring developers ended up spending more time editing the configuration than writing business features.
Spring Boot however has changed this way of programming.
Spring Boot was born with the mission of simplifying application development, working on the following areas:
- Autoconfiguration: Spring Boot is able to automatically provide a standard configuration for all the features of the Spring framework
- Starters: Checking dependencies with Spring Boot is really simple! You only need to enter the appropriate starter in your build file (Maven or Gradle) and all its dependencies will be loaded automatically.
- A solid command-line: even if not the only tool, it is possible to generate project templates very quickly using the powerful Spring Boot command line
- Metrics: Spring Boot already provides the tools, through the Actuator library, to be able to perform production monitoring of your applications
- Embedded Web Server: With the addition of a simple dependency, you can equip your application with your favorite Web server (Tomcat, Jetty or Undertow)
Starting an Hello World Spring Boot project
A Spring Boot project is similar to a normal Spring project but with the addition of starters in the dependencies and its auto-configuration. Consequently, if you are familiar with the development in Spring, it will be even easier for you to switch to Spring Boot. There are two main ways to create a project with Spring Boot:
- Installing Spring Boot CLI
- Using the Cloud Web application Spring Initializr
In this tutorial we will learn how to create a sample Hello World Spring Boot 2 application using the Spring Boot CLI. We will use the latest version of Spring Boot.
Spring Boot latest version
The latest version of Spring Boot is 3.1.3 – updated to September 2023
In order to do that, we need to download it from the following repository: http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/
Example:
wget http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/3.1.3/spring-boot-cli-3.1.3-bin.zip
Then, unzip the file:
unzip spring-boot-cli-3.1.3-bin.zip
Here is your Spring Boot home directory:
├── bin │ ├── spring │ └── spring.bat ├── INSTALL.txt ├── legal │ └── open_source_licenses.txt ├── lib │ └── spring-boot-cli-.jar ├── LICENCE.txt └── shell-completion ├── bash │ └── spring └── zsh └── _spring
I advise you to add the “bin” directory to your system PATH, so that you can run the “spring” command from anywhere:
PATH="$HOME/spring-2.6.3/bin:$PATH"
Spring Boot Hello World in Java
With the Spring CLI in place, we will now create an Hello World example project with just a simple command:
spring init -dweb helloworld
Here is the content of our helloworld folder:
helloworld ├── HELP.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main │ ├── java │ │ └── com │ │ └── example │ │ └── helloworld │ │ └── DemoApplication.java │ └── resources │ ├── application.properties │ ├── static │ └── templates └── test └── java └── com └── example └── helloworld └── DemoApplicationTests.java
Just a main Application class has been created for you:
package com.example.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Now compile and package the application with:
$ mvn clean install
Now you can run the application in any Spring Boot available way, f.e.
mvn spring-boot:run
The application will start in a few seconds:
2019-09-17 15:13:13.307 INFO 18072 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-09-17 15:13:13.310 INFO 18072 --- [ main] com.example.helloworld.DemoApplication : Started DemoApplication in 2.002 seconds (JVM running for 14.643)
Now let’s see how to create a REST application with Spring Boot: Spring Boot Hello World REST Service
Spring Boot Hello World in Groovy
Here is now a sample “Hello world” application written in Groovy which only consists of the following helloworld.groovy:
@RestController class HelloWorldSpringBoot { @RequestMapping("/") String home() { "Hello World from Spring Boot!" } }
As you can see Groovy is able to import packages and classes automatically, so you don’t have to use explicit import like in Java! (unless there is a conflict between multiple classes of the same name). To compile and run the application, run the following command:
$ spring run helloworld.groovy
It is possible to test the application, requesting the root page on the default port (8080):
$ curl localhost:8080
It is possible to pass extra arguments to the command, such as if you want to start the application on a custom port:
$ spring run helloworld.groovy -- --server.port=9000
In order to set JVM parameters it is sufficient to use the JAVA_OPTS environment variable as in this example:
$ JAVA_OPTS=-Xmx1024m spring run helloworld.groovy