Search icon
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Learning Hub
Free Learning
Arrow right icon
Over 7,000 tech titles at $9.99 each with AI-powered learning assistants on new releases
Learning Spring Boot 2.0 - Second Edition
Learning Spring Boot 2.0 - Second Edition

Learning Spring Boot 2.0: Simplify the development of lightning fast applications based on microservices and reactive programming, Second Edition

By Greg L. Turnquist , Greg L. Turnquist
$39.99 $9.99
Book Nov 2017 370 pages 2nd Edition
$39.99 $9.99
$15.99 Monthly
$39.99 $9.99
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now
Table of content icon View table of contents Preview book icon Preview Book

Learning Spring Boot 2.0 - Second Edition

Quick Start with Java

Working with Spring Boot is like pair-programming with the Spring developers.
Josh Long @starbuxman

Perhaps you've heard about Spring Boot? It's cultivated the most popular explosion in software development in years. Clocking millions of downloads per month, the community has exploded since its debut in 2013.

I hope you're ready for some fun, because we are going to take things to the next level as we use Spring Boot to build a social media platform. We'll explore its many valuable features, all the way from the tools designed to speed up development efforts to production-ready support as well as cloud-native features.

Despite some rapid fire demos you might have caught on YouTube, Spring Boot isn't just for quick demos. Built atop the de facto standard toolkit for Java, the Spring Framework, Spring Boot will help us build this social media platform with lightning speed and stability.

Also, this book will explore a new paradigm introduced in Spring Framework 5, reactive programming. In this day and age, as we build bigger systems, iterate faster, and host fleets of distributed microservices, it has become critical that we switch from a classic blocking programming style. As Josh Long would point out, this is nothing new. The network stacks of today's OSs are inherently asynchronous, but the JVM is not. Only in recent years have people realized the need to chop up tasks in a asynchronous, non-blocking fashion. However, the programming paradigm to handle potentially unlimited streams of data coming at fluctuating times requires a new programming model, which we will explore carefully alongside the power of Spring Boot itself.

In this chapter, we'll get a quick kick off with Spring Boot using the Java programming language. Maybe that makes you chuckle? People have been dissing Java for years as being slow, bulky, and not a good language for agile shops. In this chapter, we'll see how that is not the case.

In this chapter, we will cover the following topics:

  • Creating a bare project using the Spring Initializr found at
  • Exploring Spring Boot's management of third-party libraries
  • Seeing how to run our app straight inside our Integrated Development Environment (IDE) with no standalone containers
  • Using Spring Boot's property support to make external adjustments
  • Packaging our app into a self-contained, runnable JAR file
  • Deploying our app into the cloud
  • Adding out-of-the-box production-grade support tools
At any time, if you're interested in a more visual medium, feel free to check out my Learning Spring Boot [Video] at

Getting started

What is step one when we get underway with a project? We visit Stack Overflow and look for an example project to help us build our project!

Seriously, the amount of time spent adapting another project's build file, picking dependencies, and filling in other details adds up to a lot of wasted time.

No more.

At the Spring Initializr (, we can enter minimal details about our app, pick our favorite build system and the version of Spring Boot we wish to use, and then choose our dependencies off a menu. Click the Generate Project button, and we have a free-standing, ready-to-run application.

In this chapter, we'll take a quick test drive, and build a small web app. We can start by picking Gradle from the drop-down menu. Then select 2.0.0.M5 as the version of Spring Boot we wish to use.

Next, we need to pick our application's coordinates, as follows:

  • Group - com.greglturnquist.learningspringboot
  • Artifact - learning-spring-boot

Now comes the fun part. We pick the ingredients for our application, like picking off a delicious menu. If we start typing, say, Web, into the Dependencies box, we'll see several options appear. To see all the available options, click on the Switch to the full version link toward the bottom.

There are lots of overrides, such as switching from JAR to WAR, or using an older version of Java. You can also pick Kotlin or Groovy as the primary language for your application. For starters, in this day and age, there is no reason to use anything older than Java 8. JAR files are the way to go. WAR files are only needed when applying Spring Boot to an old container.

To build our social media platform, we need these few ingredients:

  • Reactive Web (embedded Netty + Spring WebFlux)
  • Reactive MongoDB (Spring Data MongoDB)
  • Thymeleaf template engine
  • Lombok (to simplify writing POJOs)

The following screenshot shows us picking these options:

With these items selected, click on Generate Project.

There are lots of other tools that leverage this site. For example, IntelliJ IDEA lets you create a new project inside the IDE, giving you the same options shown here. It invokes the website's REST API, and imports your new project. You can also interact with the site via curl or any other REST-based tool.

Now, let's unpack that ZIP file, and see what we've got. You will find the following:

  • A build.gradle build file
  • A Gradle wrapper, so there's no need to install Gradle
  • A application class
  • An file
  • A test class

We built an empty Spring Boot project. Now what? Before we sink our teeth into writing code, let's take a peek at the build file. It's quite terse, but carries some key bits.

Let's take a look, starting from the top:

    buildscript { 
      ext { 
        springBootVersion = '2.0.0.M5' 
      repositories { 
        maven { url "" } 
        maven { url "" } 
      dependencies { 
plugin:${springBootVersion}") } }

This preceding build file contains the basis for our project:

  • springBootVersion shows us we are using Spring Boot 2.0.0.M5
  • The Maven repositories it will pull from are listed next (Maven central plus Spring's snapshot and milestone repositories)
  • Finally, we see the spring-boot-gradle-plugin, a critical tool for any Spring Boot project

The first piece, the version of Spring Boot, is important. That's because Spring Boot comes with a curated list of 140 third-party library versions, extending well beyond the Spring portfolio and into some of the most commonly used libraries in the Java ecosystem. By simply changing the version of Spring Boot, we can upgrade all these libraries to newer versions known to work together. (See for a complete list.)

There is an extra project, the Spring IO Platform (, which includes an additional 134 curated versions, bringing the total to 274.

The repositories aren't as critical, but it's important to add milestones and snapshots if fetching a library that hasn't been released to Maven central, or is hosted on some vendor's local repository. Thankfully, Spring Initializr does this for us based on the version of Spring Boot selected on the site.

Finally, we have spring-boot-gradle-plugin (and there is a corresponding spring-boot-maven-plugin for Maven users). This plugin is responsible for linking Spring Boot's curated list of versions with the libraries we select in the build file. That way, we don't have to specify the version number.

Additionally, this plugin hooks into the build phase and bundles our application into a runnable über JAR, also known as a shaded or fat JAR.

Java doesn't provide a standardized way of loading nested JAR files into the classpath. Spring Boot provides the means to bundle up third-party JARs inside an enclosing JAR file, and properly load them at runtime. Read more at

With an über JAR in hand, we only need put it on a thumb drive. We can carry it to another machine, to a hundred virtual machines in the cloud, our data center, or anywhere else. It runs anywhere we can find a JVM.

Peeking a little further down in build.gradle, we can see the plugins that are enabled by default:

    apply plugin: 'java' 
    apply plugin: 'eclipse' 
    apply plugin: 'org.springframework.boot' 
    apply plugin: 'io.spring.dependency-management' 
  • The java plugin indicates the various tasks expected for a Java project
  • The eclipse plugin helps generate project metadata for Eclipse users
  • The org.springframework.boot plugin is where the actual spring-boot-gradle-plugin is activated
  • The io.spring.dependency-management plugin supports Maven Bill of Materials (BOM) manifests, allowing usage of libraries that manage the sets of library versions in our Gradle build. (Because Maven supports this natively, there is no Maven equivalent plugin.)
An up-to-date copy of IntelliJ IDEA can read a plain old Gradle-build file just fine without extra plugins.

This brings us to the final ingredient used to build our application--Dependencies.

Spring Boot starters

No application is complete without specifying dependencies. A valuable feature of Spring Boot is its virtual packages. These are published packages that don't contain any code, but simply list other dependencies instead.

The following code shows all the dependencies we selected on the Spring Initializr site:

    dependencies { 
mongodb-reactive') compile('org.springframework.boot:spring-boot-starter-thymeleaf') compile('org.springframework.boot:spring-boot-starter-webflux') compile('org.projectlombok:lombok') compile('de.flapdoodle.embed:de.flapdoodle.embed.mongo') testCompile('org.springframework.boot:spring-boot-starter-test') }

You might have noticed that most of these packages are Spring Boot starters:

  • spring-boot-starter-data-mongodb-reactive pulls in Spring Data MongoDB with the reactive bits enabled
  • spring-boot-starter-thymeleaf pulls in the Thymeleaf template engine
  • spring-boot-starter-webflux pulls in Spring WebFlux, Jackson JSON support, and embedded Netty

These starter packages allow us to quickly grab the bits we need to get up and running. Spring Boot starters have become so popular that many other third-party library developers are crafting their own.

In addition to starters, we have the following three extra libraries:

  • Project Lombok ( makes it dead simple to define POJOs without getting bogged down in getters, setters, and other details.
  • Flapdoodle is an embedded MongoDB database that allows us to write tests, tinker with a solution, and get things moving before getting involved with an external database.
At the time of writing, Flapdoodle isn't listed on the website. We must add it manually, as shown previously.
  • spring-boot-starter-test pulls in Spring Boot Test, JSONPath, JUnit, AssertJ, Mockito, Hamcrest, JSONassert, and Spring Test, all within test scope.

The value of this last starter, spring-boot-starter-test, cannot be overstated. With a single line, the most powerful test utilities are at our fingertips, allowing us to write unit tests, slice tests, and full-blown our-app-inside-embedded-Netty tests. It's why this starter is included in all projects without checking a box on the Spring Initializr site.

Now, to get things off the ground, we need to shift focus to the tiny bit of code written for us by the Spring Initializr.

Running a Spring Boot application

The fabulous website created a tiny class, LearningSpringBootApplication, as shown here:

    package com.greglturnquist.learningspringboot; 
    import org.springframework.boot.SpringApplication; 
org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class LearningSpringBootApplication { public static void main(String[] args) { LearningSpringBootApplication.class, args); } }

This preceding tiny class is actually a fully operational web application!

  • The @SpringBootApplication annotation tells Spring Boot, when launched, to scan recursively for Spring components inside this package and register them. It also tells Spring Boot to enable autoconfiguration, a process where beans are automatically created based on classpath settings, property settings, and other factors. We'll see more of this throughout the book. Finally, it indicates that this class itself can be a source for Spring bean definitions.
  • It holds public static void main(), a simple method to run the application. There is no need to drop this code into an application server or servlet container. We can just run it straight up, inside our IDE. The amount of time saved by this feature, over the long haul, adds up fast.
  • points Spring Boot at the leap-off point--​in this case, this very class. But it's possible to run other classes.

This little class is runnable. Right now! In fact, let's give it a shot:

      .   ____          _            __ _ _
    /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
    :: Spring Boot ::  (v2.0.0.M5)
    2017-08-02 15:34:22.374: Starting LearningSpringBootApplication
on ret...
2017-08-02 15:34:22.377: Running with Spring Boot
2017-08-02 15:34:22.378: No active profile set, falling back
to defaul...
2017-08-02 15:34:22.433: Refreshing
2017-08-02 15:34:23.717: HV000184: ParameterMessageInterpolator
has be...
2017-08-02 15:34:23.815: HV000184: ParameterMessageInterpolator
has be...
2017-08-02 15:34:23.953: Cannot find template location:
2017-08-02 15:34:24.094: Mapped URL path [/webjars/**] onto
handler of...
2017-08-02 15:34:24.094: Mapped URL path [/**] onto handler of
type [c...
2017-08-02 15:34:24.125: Looking for @ControllerAdvice:
2017-08-02 15:34:24.501: note: noprealloc may hurt performance
in many...
2017-08-02 15:34:24.858: 2017-08-02T15:34:24.858-0500 I
NETWORK [init...
2017-08-02 15:34:24.858: start
2017-08-02 15:34:24.908: Cluster created with settings
2017-08-02 15:34:24.908: Adding discovered server
localhost:65485 to c...
2017-08-02 15:34:25.007: 2017-08-02T15:34:25.006-0500 I
NETWORK [init...
2017-08-02 15:34:25.038: Opened connection
2017-08-02 15:34:25.040: Monitor thread successfully
connected to serv...
2017-08-02 15:34:25.041: Discovered cluster type of STANDALONE 2017-08-02 15:34:25.145: Cluster created with settings
2017-08-02 15:34:25.145: Adding discovered server
localhost:65485 to c...
2017-08-02 15:34:25.153: Opened connection
2017-08-02 15:34:25.153: Monitor thread successfully connected
to serv...
2017-08-02 15:34:25.153: Discovered cluster type of STANDALONE 2017-08-02 15:34:25.486: Registering beans for JMX exposure
on startup
2017-08-02 15:34:25.556: Started HttpServer on
2017-08-02 15:34:25.558: Netty started on port(s): 8080 2017-08-02 15:34:25.607: Started in 3.617 seconds (JVM
running for 4.0...

Scrolling through the preceding output, we can see these several things:

  • The banner at the top gives us a read-out of the version of Spring Boot. (By the way, you can create your own ASCII art banner by creating either banner.txt or banner.png and putting it in the src/main/resources/ folder.)
  • Embedded Netty is initialized on port 8080, indicating that it's ready for web requests.
  • It's slightly cut off, but there are signs that Flapdoodle, our embedded MongoDB data store, has come up.
  • And the wonderful Started LearningSpringBootApplication in 3.617 seconds message can be seen too.

Spring Boot uses embedded Netty, so there's no need to install a container on our target machine. Non-web apps don't even require that. The JAR itself is the new container that allows us to stop thinking in terms of old-fashioned servlet containers. Instead, we think in terms of apps. All these factors add up to maximum flexibility in application deployment.

How does Spring Boot use embedded Netty among other things? As mentioned earlier, it has autoconfiguration, which means that it defines Spring beans based on different conditions. When Spring Boot sees Netty on the classpath, it creates an embedded Netty instance along with several beans to support it.

When it spots Spring WebFlux on the classpath, it creates view resolution engines, handler mappers, and a whole host of other beans needed to help us write a web application. This lets us focus writing routes, not doddling around configuring infrastructure.

With Flapdoodle on the classpath as well as the Reactive MongoDB drivers, it spins up an in-memory, embedded MongoDB data store and connects to it with its state-of-the-art drivers.

Spring Data MongoDB will cause Spring Boot to craft a MongoOperations bean along with everything else needed to start speaking Mongo Query Language and make it available if we ask for it, letting us focus on defining repositories.

At this stage, we have a running web application, albeit an empty one. There are no custom routes, and no means to handle data. But we can add some real fast.

Let's start by drafting a simple REST controller as follows:

    package com.greglturnquist.learningspringboot; 
    import org.springframework.web.bind.annotation.GetMapping; 
    import org.springframework.web.bind.annotation.RequestParam; 
    import org.springframework.web.bind.annotation.RestController; 
    public class HomeController { 
      public String greeting(@RequestParam(required = false, 
      defaultValue = "") String name) { 
        return name.equals("") ? "Hey!" : "Hey, " + name + "!"; 

Let's examine this tiny REST controller in detail:

  • The @RestController annotation indicates that we don't want to render views, but write the results straight into the response body instead.
  • @GetMapping is Spring's shorthand annotation for @RequestMapping(method = RequestMethod.GET). In this case, it defaults the route to /.
  • Our greeting() method has one argument--@RequestParam(required=false, defaultValue="") String name. It indicates that this value can be requested via an HTTP query (?name=Greg)--the query isn't required, and in case it's missing, it will supply an empty string.
  • Finally, we return one of two messages depending on whether or not the name is an empty string, using Java's ternary operator.

If we relaunch LearningSpringBootApplication in our IDE, we'll see this new entry in the console:

    2017-08-02 15:40:00.741: Mapped "{[],methods=[GET]}" onto
public java....

We can then ping our new route in the browser at http://localhost:8080 and http://localhost:8080?name=Greg. Try it out!

(By the way, it sure would be handy if the system could detect this change and relaunch automatically, right? Check out Chapter 5, Developer Tools for Spring Boot Apps to find out how.)

That's nice, but since we picked Spring Data MongoDB, how hard would it be to load some sample data and retrieve it from another route? (Spoiler alert--Not hard at all.)

We can start out by defining a simple Chapter entity to capture book details, as follows:

    package com.greglturnquist.learningspringboot; 
    import lombok.Data; 
    public class Chapter { 
      private String id; 
      private String name; 
      public Chapter(String name) { = name; 

This preceding little POJO lets us look at the details about the chapter of a book as follows:

  • The @Data annotation from Lombok generates getters, setters, a toString() method, an equals() method, a hashCode() method, and a constructor for all required (that is, final) fields
  • The @Document annotation flags this class as suitable for storing in a MongoDB data store
  • The id field is marked with Spring Data's @Id annotation, indicating this is the primary key of our Mongo document
  • Spring Data MongoDB will, by default, create a collection named chapters with two fields, id and name
  • Our field of interest is name, so let's create a constructor call to help insert some test data

To interact with this entity and its corresponding collection in MongoDB, we could dig in and start using the autoconfigured MongoOperations supplied by Spring Boot. But why do that when we can declare a repository-based solution?

To do this, we'll create an interface defining the operations we need. Check out this simple interface:

    package com.greglturnquist.learningspringboot; 
.reactive.ReactiveCrudRepository; public interface ChapterRepository extends ReactiveCrudRepository<Chapter, String> { }

This last declarative interface creates a Spring Data repository as follows:

  • ReactiveCrudRepository extends Repository, a Spring Data Commons marker interface that signals Spring Data to create a concrete implementation based on the reactive paradigm while also capturing domain information. It also comes with some predefined CRUD operations (save, delete, deleteById, deleteAll, findById, findAll, and more).
  • It specifies the entity type (Chapter) and the type of the primary key (String).
  • We could also add custom finders, but we'll save that for Chapter 3, Reactive Data Access with Spring Boot.

Spring Data MongoDB will automatically wire up a concrete implementation of this interface.

Spring Data doesn't engage in code generation. Code generation has a sordid history of being out of date at some of the worst times. Instead, Spring Data uses proxies and other mechanisms to support these operations. Never forget--​the code you don't write has no bugs.

With Chapter and ChapterRepository defined, we can now preload the database, as shown in the following code:

    package com.greglturnquist.learningspringboot; 
    import reactor.core.publisher.Flux; 
    import org.springframework.boot.CommandLineRunner; 
    import org.springframework.context.annotation.Bean; 
    import org.springframework.context.annotation.Configuration; 
    public class LoadDatabase { 
      CommandLineRunner init(ChapterRepository repository) { 
        return args -> { 
            new Chapter("Quick Start with Java"), 
            new Chapter("Reactive Web with Spring Boot"), 
            new Chapter("...and more!")) 

This preceding class will be automatically scanned by Spring Boot and run in the following way:

  • @Configuration marks this class as a source of beans.
  • @Bean indicates that the return value of init() is a Spring Bean--​in this case, a CommandLineRunner (utility class from Spring Boot).
  • Spring Boot runs all CommandLineRunner beans after the entire application is up and running. This bean definition requests a copy of ChapterRepository.
  • Using Java 8's ability to coerce the args → {} lambda function into CommandLineRunner, we are able to gather a set of Chapter data, save all of them and then print them out, preloading our data.
We aren't going to delve into the intricacies of Flux, flatMap, and subscribe yet. We'll save that for Chapter 2, Reactive Web with Spring Boot and Chapter 3, Reactive Data Access with Spring Boot.

With all this in place, the only thing left is to write a REST controller to serve up the data!

    package com.greglturnquist.learningspringboot; 
    import reactor.core.publisher.Flux; 
    import org.springframework.web.bind.annotation.GetMapping; 
    import org.springframework.web.bind.annotation.RestController; 
    public class ChapterController { 
      private final ChapterRepository repository; 
      public ChapterController(ChapterRepository repository) { 
        this.repository = repository; 
      public Flux<Chapter> listing() { 
        return repository.findAll(); 

This preceding controller is able to serve up our data as follows:

  • @RestController indicates that this is another REST controller.
  • Constructor injection is used to automatically load it with a copy of ChapterRepository. With Spring, if there is only one constructor call, there is no need to include an @Autowired annotation.
  • @GetMapping tells Spring that this is the place to route /chapters calls. In this case, it returns the results of the findAll() call found in ReactiveCrudRepository. Again, if you're curious what Flux<Chapter> is, we'll tackle that at the top of the next chapter. For now, think of it being like a Stream<Chapter>.

If we relaunch our application and visit http://localhost:8080/chapters, we can see our preloaded data served up as a nicely formatted JSON document, as seen in this screenshot:

This may not be very elaborate, but this small collection of classes has helped us quickly define a slice of functionality. And, if you'll notice, we spent zero effort configuring JSON converters, route handlers, embedded settings, or any other infrastructure.

Spring Boot is designed to let us focus on functional needs, not low-level plumbing.

Delving into Spring Boot's property support

We just got things off the ground with an operational application, but that isn't the only killer feature of Spring Boot.

Spring Boot comes with a fistful of prebuilt properties. In fact, just about every autoconfigured component has some property setting ( allowing you to override just the parts you like.

Many of these autoconfigured beans will back off if Boot spots us creating our own. For example, when Spring Boot spots reactive MongoDB drivers on the classpath, it automatically creates a reactive MongoClient. However, if we define our own MongoClient bean, then Spring Boot will back off and accept ours.

This can lead to other components switching off. But sometimes, we don't need to swap out an entire bean. Instead, we may wish to merely tweak a single property of one of these autoconfigured beans.

Let's try to make some adjustments to src/main/resources/ as follows:

    # Override the port Tomcat listens on 
    # Customize log levels 

This preceding changes will cause Spring Boot to launch Netty on port 9000, as shown here:

2017-08-02 15:40:02.489: Netty started on port(s): 9000  

It will also bump up the log level for package com.greglturnquist to DEBUG.

Many modern IDEs include code completion to find various properties.

While it's handy to externalize configuration settings into property files, it wouldn't be a big advantage if they were only embeddable inside our app's JAR file.

That's why, Spring Boot comes with property override support. The following list shows all the locations from which we can override properties, the first being the highest priority:

  • The @TestPropertySource annotation on test classes
  • Command-line arguments
  • The properties found inside SPRING_APPLICATION_JSON (inline JSON embedded in an env variable or system property)
  • The ServletConfig init parameters
  • The ServletContext init parameters
  • The JNDI attributes from java:comp/env
  • The Java System properties (System.getProperties())
  • The OS environment variables
  • A RandomValuePropertySource that only has properties in random.*
  • Profile-specific properties outside the packaged JAR file (application-{profile}.properties and YAML variants)
  • Profile-specific properties inside the packaged JAR file (application-{profile}.properties and YAML variants)
  • Application properties outside the package JAR file ( and YAML variants)
  • Application properties inside the packaged JAR file ( and YAML variants)
  • The @PropertySource annotation on any @Configuration classes
  • Default properties (specified using SpringApplication.setDefaultProperties)

For an example of the same overrides in YAML format as our file, we could put the following in application.yml in src/main/resources:

      port: 9000
          greglturnquist: DEBUG  

This would do the exact same thing that we already saw with The only difference is the formatting.

What are the benefits of YAML over properties? If we need to override lots of settings, it avoids duplication of various keys.

Spring properties can also reference other properties, as shown in this fragment:
    app.description=${} is a Spring Boot application  

In this preceding example, the second property, app.description, references the first property,

This isn't the end of options with property overrides. It's just the beginning. Throughout this book, we'll expand on the options provided by Spring Boot's property support.

For now, let's focus on getting our app to production!

Bundling up the application as a runnable JAR file

We've hacked out a suitable application. Now it's time to take it to production. As Spring Developer Advocate Josh Long likes to say, production is the happiest place on earth.

The good ol' spring-boot-gradle-plugin has built-in hooks to handle that for us. By invoking Gradle's build task, it will insert itself into the build process, and create a JAR file.

$ ./gradlew clean build
:processTestResources UP-TO-DATE
... test output ...
Total time: 10.946 secs  

If we peek at the output, we'll find the original JAR file (non-FAT) along with the rebundled one containing our application code as well as the third-party dependencies, as shown here:

$ ls build/libs
If you wish to check out the newly minted JAR's contents, type jar tvf build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar. We won't show it here because of space constraints.

The über JAR is nicely loaded up with our custom code, all of our third-party dependencies, and a little Spring Boot code to allow us to run it. Why not try that out right here?

Let's type the following command:

$ java -jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar  

We can expect the same output as before, which is as seen in this image:

      .   ____          _            __ _ _
    /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
    :: Spring Boot ::  (v2.0.0.M5)
    2017-09-19 20:41:20.036: Starting LearningSpringBootApplication 
on ret...
... ... the rest has been cut for space ...

By invoking the JAR using Java's -jar option, we can launch the application with nothing more than the JVM on our machine.

With our JAR file in hand, we can take our application anywhere. If we need to override any settings, we can do it without cracking it open and making alterations.

Suppose we alter our command slightly, like this:

$ SERVER_PORT=8000 java
-jar build/libs/learning-spring-boot-0.0.1-SNAPSHOT.jar

We can now expect the results to be a little different, as seen in this image:

      .   ____          _            __ _ _
    /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
    :: Spring Boot ::  (v2.0.0.M5)
    2017-08-03 15:40:02.489: Netty started on port(s): 8000

From the command line, we override server.port using an alternative notation (SERVER_PORT) and run it on port 8000.

This lends us the ability to deploy it into the cloud.

Deploying to Cloud Foundry

Cloud-native applications are becoming the norm, as companies accelerate their rate of releasing to production (

Cloud Native describes the patterns of high performing organizations delivering software faster, consistently and reliably at scale. Continuous delivery, DevOps, and microservices label the why, how and what of the cloud natives. In the the most advanced expression of these concepts they are intertwined to the point of being inseparable. Leveraging automation to improve human performance in a high trust culture, moving faster and safer with confidence and operational excellence.

Many cloud platforms thrive under releasing self-contained applications. The open source Cloud Foundry platform, with its support for many technologies and runnable JAR files, is one of the most popular ones.

To get started, we need either a copy of Cloud Foundry installed in our data center, or an account at Pivotal Web Services (PWS), a Cloud Foundry hosting provider ( Assuming we have a PWS account (pronounced p-dubs), let's install the tools and deploy our app.

On macOS X, we can type this:

$ brew tap cloudfoundry/tap
$ brew install cf-cli
=> Installing cf-cli from cloudfoundry/tap
==> Downloading
==> Downloading from
==> Caveats Bash completion has been installed to: /usr/local/etc/bash_completion.d ==> Summary /usr/local/Cellar/cf-cli/6.32.0: 6 files, 16.7MB,
built in 10 seco...

For Linux, we can fetch a tarball like this:

$ wget -O cf-linux.tgz "
$ tar xvfz cf-linux.tgz $ chmod 755 ./cf

This preceding code will download and enable a Linux-based cf tool.

Before using the cf tool, you must register for an account at PWS.

For more installation details, visit

Using the cf tool, let's deploy our application. To kick things off, we need to log into PWS, as follows:

$ cf login
API endpoint:
Select an org (or press enter to skip):
... your organizations will be listed here ...
Org> 2
Targeted org FrameworksAndRuntimes
Select a space (or press enter to skip):
... your spaces will be listed here ...
Space> 1
Targeted space development
API endpoint: (API version: 2.62.0)
Org:            FrameworksAndRuntimes
Space:          development  

We are logged in and targeting a logical space inside an organization.

Your Org and Space will certainly be different.

Time to deploy! We can do so with the cf push command. At a minimum, we specify the name of our application and the artifact with the -p option (and use a different name than learning-spring-boot, since it's been taken by this book!):

$ cf push learning-spring-boot -p build/libs/learning-spring-boot-
Creating app learning-spring-boot in org FrameworksAndRuntimes
/ space development as
OK Creating route
OK Binding to learning-spring-boot... OK Uploading learning-spring-boot... ... ... Staging complete Uploading droplet, build artifacts cache... Uploading build artifacts cache... Uploading droplet... Uploaded build artifacts cache (108B) Uploaded droplet (76.7M) Uploading complete Destroying container Successfully destroyed container 0 of 1 instances running, 1 starting 0 of 1 instances running, 1 starting 0 of 1 instances running, 1 starting 1 of 1 instances running App started OK ... ... requested state: started instances: 1/1 usage: 1G x 1 instances urls: last uploaded: Tue Sep 20 02:01:13 UTC 2017 stack: cflinuxfs2 buildpack: java-buildpack=v3.9-offline- java-main
open-jdk-like-jre=1.8.0_101 open-jdk-like-memory-
calculator=2.0.2_RELEASE spring-auto-reconfiguration=1.10.0_RELEASE
state since cpu memory disk #0 running 2017-09-19 09:01:59 PM 243.7% 503.5M of 1G 158.1M of 1G


We have pushed our JAR file to PWS, let the Java buildpack (automatically selected) register it with a URL, and start it up. Now, we can visit its registered URL at

$ curl
  Hey, Greg!  

We've taken our application to production.

The next step is to handle what are sometimes referred to as Day 2 situations. This is where we must now monitor and maintain our application, and Spring Boot is ready to provide us just what we need.

Adding production-ready support

We've created a Spring web app with minimal code and released it to production. This is the perfect time to introduce production-grade support features.

There are some questions that often arise in production, and these are as follows:

  • What do we do when the system administrator wants to configure his or her monitoring software to ping our app to see if it's up?
  • What happens when our manager wants to know the metrics of people hitting our app?
  • What are we going to do when the ops center supervisor calls us at 2:00 a.m. and we have to figure out what went wrong?

The last feature we are going to introduce in this chapter is Spring Boot's Actuator module. This module provides some super slick Ops-oriented features that are incredibly valuable in a production environment.

We start by adding this dependency to our build.gradle as follows:


When you run this version of our app, the same business functionality is available that we saw earlier, but there are additional HTTP endpoints; these are listed in the following table:

Actuator Endpoint



This reports what Spring Boot did and didn't autoconfigure, and why


This reports all the beans configured in the application context (including ours as well as the ones autoconfigured by Boot)


This exposes all configuration properties


This creates thread dump report


This reports on the current system environment


This is a simple endpoint to check the life of the app


This serves up custom content from the app


This shows counters and gauges on web usage


This gives us details about all Spring WebFlux routes


This shows the details about past requests

Endpoints, by default, are disabled. We have to opt in. This is accomplished by setting endpoints.{endpoint}.enabled=true inside src/main/resources/, like this:

This line added to mentions the endpoint, health, and enables it. If we restart the application, we can ping for its health, as shown in the next section.

Pinging our app for general health

Each of these endpoints can be visited using our browser or using other tools like curl:

$ curl localhost:9000/application/health
"status": "UP",
"details": {
"mongo": {
"status": "UP",
"details": {
"version": "3.2.2"
"diskSpace": {
"status": "UP",
"details": {
"total": 498937626624,
"free": 66036432896,
"threshold": 10485760

This preceding health status gives us the following:

  • An overall UP status
  • The status of MongoDB
  • The status of the diskspace

When other components are added, they may, optionally, add their own health checks.

This immediately solves our first need listed previously. We can inform the system administrator that he or she can write a management script to interrogate our app's health.

Be warned that each of these endpoints serve up a compact JSON document. Generally speaking, command-line curl probably isn't the best option. While it's convenient on *nix and Mac systems, the content is dense and hard to read. It's more practical to have the following:


To really get operational, we need metrics. Most production systems have metrics in one form or another. Thankfully, we don't have to start from scratch. There is a metric endpoint in Spring Boot Actuator. If we add this following setting to


With this property setting, if we restart the application, we can get a quick read out on thing.

Assuming we have JSON Viewer installed, it's easy to surf to http://localhost:9000/application/metrics and get a listing on all sorts of metrics. We even have counters for every good/bad web hit, broken down on a per-page basis, as shown here:

"names": [

We can visit any one of these metrics by appending it's name to the metrics URL. For example, to view the http.server.requests, visit http://localhost:9000/application/metrics/http.server.requests:

"name": "http.server.requests",
"measurements": [
"statistic": "TotalTime",
"value": 3.53531643E8
"statistic": "Count",
"value": 57.0
"availableTags": [
"tag": "exception",
"values": [
"tag": "method",
"values": [
"tag": "uri",
"values": [
"tag": "status",
"values": [

This provides a basic framework of metrics to satisfy our manager's needs. It's important to understand that metrics gathered by Spring Boot Actuator aren't persistent across application restarts. To gather long-term data, we have to write them elsewhere (

If you have used Spring Boot 1.x, then this may look very different. That's because a newer, more sophisticated version of metrics has arrived--Micrometer. It's currently in development, and may change quite a bit, so stay tuned at, and be sure to follow @micrometerio on Twitter, as the ability to craft highly detailed and advanced metrics comes to Spring Boot.


In this chapter, we rapidly crafted a Spring Web application using the Spring stack on top of Netty with little configuration from our end. We plugged in Spring Boot's Actuator module, configuring it with metrics, health, and management features so that we can monitor it in production by merely adding two lines of extra code.

In the next chapter, we'll get underway building our social media platform using these scalable APIs built on top of Reactive Streams.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Get up to date with the defining characteristics of Spring Boot 2.0 in Spring Framework 5
  • Learn to perform Reactive programming with SpringBoot
  • Learn about developer tools, AMQP messaging, WebSockets, security, MongoDB data access, REST, and more


Spring Boot provides a variety of features that address today's business needs along with today's scalable requirements. In this book, you will learn how to leverage powerful databases and Spring Boot's state-of-the-art WebFlux framework. This practical guide will help you get up and running with all the latest features of Spring Boot, especially the new Reactor-based toolkit. The book starts off by helping you build a simple app, then shows you how to bundle and deploy it to the cloud. From here, we take you through reactive programming, showing you how to interact with controllers and templates and handle data access. Once you're done, you can start writing unit tests, slice tests, embedded container tests, and even autoconfiguration tests. We go into detail about developer tools, AMQP messaging, WebSockets, security, and deployment. You will learn how to secure your application using both routes and method-based rules. By the end of the book, you'll have built a social media platform from which to apply the lessons you have learned to any problem. If you want a good understanding of building scalable applications using the core functionality of Spring Boot, this is the book for you.

What you will learn

[*] Create powerful, production-grade applications and services with minimal fuss [*] Support multiple environments with one artifact, and add production-grade support with features [*] Find out how to tweak your apps through different properties [*] Use custom metrics to track the number of messages published and consumed [*] Enhance the security model of your apps [*] Make use of reactive programming in Spring Boot [*] Build anything from lightweight unit tests to fully running embedded web container integration tests

Product Details

Country selected

Publication date : Nov 3, 2017
Length 370 pages
Edition : 2nd Edition
Language : English
ISBN-13 : 9781786463784
Vendor :
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details

Publication date : Nov 3, 2017
Length 370 pages
Edition : 2nd Edition
Language : English
ISBN-13 : 9781786463784
Vendor :
Category :
Languages :

Table of Contents

11 Chapters
Preface Chevron down icon Chevron up icon
1. Quick Start with Java Chevron down icon Chevron up icon
2. Reactive Web with Spring Boot Chevron down icon Chevron up icon
3. Reactive Data Access with Spring Boot Chevron down icon Chevron up icon
4. Testing with Spring Boot Chevron down icon Chevron up icon
5. Developer Tools for Spring Boot Apps Chevron down icon Chevron up icon
6. AMQP Messaging with Spring Boot Chevron down icon Chevron up icon
7. Microservices with Spring Boot Chevron down icon Chevron up icon
8. WebSockets with Spring Boot Chevron down icon Chevron up icon
9. Securing Your App with Spring Boot Chevron down icon Chevron up icon
10. Taking Your App to Production with Spring Boot Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by

No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial


How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to
  • To contact us directly if a problem is not resolved, use
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.