Quick Start with Java
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 http://start.spring.io
- 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
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 (https://start.spring.io), 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.
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.
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 LearningSpringBootApplication.java application class
- An application.properties file
- A LearningSpringBootApplicationTests.java 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:
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 https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-dependencies/pom.xml for a complete list.)
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.
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:
- 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.)
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:
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 (https://projectlombok.org) 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.
- 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.