Introducing Spring Boot
Introducing Spring Boot
You can use Spring Boot to create Java applications that can
be started by using java -jar or more traditional war
deployments. We also provide a command line tool that runs
“spring scripts”.
You can use Spring Boot to create Java applications that can
be started by using java -jar or more traditional war
deployments. We also provide a command line tool that runs
“spring scripts”.
$ java -version
10.1.1 Maven Installation
Spring Boot is compatible with Apache Maven 3.3 or above. If
you do not already have Maven installed, you can follow the
instructions at maven.apache.org.
On many operating systems, Maven can be installed with a package manager. If you
use OSX Homebrew, try brew install maven. Ubuntu users can run sudo
apt-get install maven. Windows users with Chocolatey can run choco
install maven from an elevated (administrator) prompt.
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
Gradle Wrapper
You do not need to use the CLI to work with Spring Boot, but it
is definitely the quickest way to get a Spring application off
the ground.
10.2.1 Manual Installation
You can download the Spring CLI distribution from the Spring
software repository:
spring-boot-cli-2.1.3.RELEASE-bin.zip
spring-boot-cli-2.1.3.RELEASE-bin.tar.gz
If you develop features for the CLI and want easy access to
the version you built, use the following commands:
$ sdk ls springboot
=========================================================================
=======
Available Springboot Versions
=========================================================================
=======
> + dev
* 2.1.3.RELEASE
=========================================================================
=======
+ - local version
* - installed
> - currently in use
=========================================================================
=======
If you do not see the formula, your installation of brew might be out-of-date. In that
case, run brew update and try again.
10.2.4 MacPorts Installation
If you are on a Mac and use MacPorts, you can install the
Spring Boot CLI by using the following command:
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring <HIT TAB HERE>
grab help jar run test version
If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-
line completion scripts are automatically registered with your shell.
10.2.6 Windows Scoop Installation
If you are on a Windows and use Scoop, you can install the
Spring Boot CLI by using the following commands:
If you do not see the app manifest, your installation of scoop might be out-of-date.
In that case, run scoop update and try again.
10.2.7 Quick-start Spring CLI Example
You can use the following web application to test your
installation. To start, create a file called app.groovy , as follows:
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
Hello World!
Once you’re done with the migration, please make sure to remove this module from
your project’s dependencies.
$ java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -v
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-
17T14:33:14-04:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation
This sample needs to be created in its own folder. Subsequent instructions assume
that you have created a suitable folder and that it is your current directory.
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
</project>
The preceding listing should give you a working build. You can
test it by running mvn package (for now, you can ignore the “jar
will be empty - no content was marked for inclusion!” warning).
At this point, you could import the project into an IDE (most modern Java IDEs
include built-in support for Maven). For simplicity, we continue to use a plain text
editor for this example.
$ mvn dependency:tree
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
If you run mvn dependency:tree again, you see that there are now
a number of additional dependencies, including the Tomcat
web server and Spring Boot itself.
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
Although there is not much code here, quite a lot is going on.
We step through the important parts in the next few sections.
11.3.1 The @RestController and @RequestMapping
Annotations
The first annotation on our Example class is @RestController .
This is known as a stereotype annotation. It provides hints for
people reading the code and for Spring that the class plays a
specific role. In this case, our class is a web @Controller , so
Spring considers it when handling incoming web requests.
$ mvn spring-boot:run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)
If you open a web browser to localhost:8080 , you should see the
following output:
Hello World!
To gracefully exit the application, press ctrl-c .
Java does not provide a standard way to load nested jar files
(jar files that are themselves contained within a jar). This can
be problematic if you are looking to distribute a self-contained
application.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The spring-boot-starter-parent POM
includes <executions> configuration to bind the repackage goal. If you do not
use the parent POM, you need to declare this configuration yourself. See the plugin
documentation for details.
Save your pom.xml and run mvn package from the command line,
as follows:
$ mvn package
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)