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, such as the JBoss middleware, 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 autoconfiguration. 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. 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/2.1.0.RELEASE/spring-boot-cli-2.1.8.RELEASE-bin.zip

Then, unzip the file:

unzip spring-boot-cli-2.1.8.RELEASE-bin.zip

Here is your Spring Boot home directory:

tree spring-2.1.8.RELEASE/
spring-2.1.8.RELEASE/
├── bin
│ ├── spring
│ └── spring.bat
├── INSTALL.txt
├── legal
│ └── open_source_licenses.txt
├── lib
│ └── spring-boot-cli-2.1.8.RELEASE.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.

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

Spring Boot Hello World in Java

In order to create an Hello World example project, we will use the Spring Boot CLI

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

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials