Preface
The Spring Framework provides great flexibility for Java development, which also results in tedious configuration work. Spring Boot addresses the configuration difficulties of Spring and makes it easy to create standalone, production-grade Spring-based applications. This practical guide makes the existing development process more efficient. Spring Boot Cookbook 2.0, Second Edition smartly combines all the skills and expertise to efficiently develop, test, deploy, and monitor applications using Spring Boot on-premises and in the cloud. We start with an overview of the important Spring Boot features you will learn to create a web application for a RESTful service. You will also learn how to fine-tune the behavior of a web application by learning about custom routes and asset paths and how to modify routing patterns along with addressing the requirements of a complex enterprise application and understanding the creation of custom Spring Boot starters. This book also includes examples of the new and improved facilities available to create the various kinds of tests introduced in Spring Boot 1.4 and 2.0 and gain insights into Spring Boot DevTools. We will explore the basics of Spring Boot Cloud modules and various cloud starters to make cloud-native applications and take advantage of service discovery and circuit breakers.
Who this book is for
This book is targeted at Java Developers who have a good knowledge and understanding of Spring and Java application development, are familiar with the notions of the Software Development Life Cycle (SDLC), and understand the need of different kinds of testing strategies, general monitoring, and deployment concerns. This book will help you learn efficient Spring Boot development techniques and integration and extension capabilities in order to make the existing development process more efficient.
What this book covers
Chapter 1, Getting Started with Spring Boot, provides an overview of the important and useful Spring Boot starters that are included in the framework. You will learn how to use spring.io resources, how to get started with a simple project, and how to configure the build file to contain their desired starters. The chapter will finish with creating a simple command-line application configured to execute some scheduled tasks.
Chapter 2, Configuring Web Applications, provides examples of how to create and add custom servlet filters, interceptors, converters, formatters, and PropertyEditors to a Spring Boot web application. It will start by creating a new web application and use it as a base to customize with the components we discuss earlier in the chapter.
Chapter 3, Web Framework Behavior Tuning, delves into fine-tuning the behavior of a web application. It will cover configuring custom routing rules and patterns, adding additional static asset paths, and adding and modifying servlet container connectors and other properties, such as enabling SSL.
Chapter 4, Writing Custom Spring Boot Starters, shows how to create custom Spring Boot starters to provide additional behaviors and functionality that might be required for complex enterprise applications. You will learn about how the autoconfiguration mechanics works under the hood and how to use them to selectively enable/disable default functionality and conditionally load your own.
Chapter 5, Application Testing, explores different techniques for testing Spring Boot applications. It will start by introducing you to testing MVC applications, then proceed with some tips on how to use in-memory databases with pre-populated data to mimic real DB interactions during tests, and conclude with examples of BDD via testing tools such as Cucumber and Spock.
Chapter 6, Application Packaging and Deployment, will cover examples of configuring your build to produce Docker images and self-executing binary files for Linux/OSX environments. We will explore the options for external application configuration using Consul and delve into the details of Spring Boot environment and configuration functionality.
Chapter 7, Health Monitoring and Data Visualization, explores the various mechanisms that Spring Boot provides to help us see data relating to application's health. We will start by learning how to write and expose custom health metrics and see the data using HTTP endpoints and JMX. It will then proceed with the overview and creation of management commands for SSHd and finish with integration of monitoring data with Graphite and Dashing using the Micrometer metrics framework.
Chapter 8, Spring Boot DevTools, provides an insight into how to use Spring Boot DevTools during application development to simplify common tasks of dynamic code recompiling/restarting and remote code updating. We will learn how to add DevTools to the project, followed by exploring how DevTools helps speed up the development process by automatically restarting a running application when code changes take place.
Chapter 9, Spring Cloud, provides examples of various features in Spring Boot Cloud modules. You will learn how to use different cloud modules for service discovery, such as Consul or Netflix Eureka. Later, we will look at how to incorporate Netflix libraries such as the Hystrix circuit breaker and the Feign interface-based REST client.
To get the most out of this book
For this book, you need to have JDK 1.8 installed on your favorite operating system, Linux, Windows, or OS X. It is assumed that readers have reasonable familiarity with Java, including the latest features added by JDK 1.8, as well as basic knowledge of Spring Framework and its operating concepts, such as dependency injection, inversion of control, and MVC.
The rest of the software, such as the Gradle build tool, all the necessary Java libraries, such as Spring Boot, Spring Framework, and its dependencies, as well as Docker, Consul, Graphite, Grafana, and Dashing will be all installed throughout the recipes.
Download the example code files
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
- Log in or register at www.packtpub.com.
- Select the
SUPPORT
tab. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR/7-Zip for Windows
- Zipeg/iZip/UnRarX for Mac
- 7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Spring-Boot-2.0-Cookbook-Second-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Now we will add a @Bean
declaration for LocaleChangeInterceptor
."
A block of code is set as follows:
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
Any command-line input or output is written as follows:
$ ./gradlew clean bootRun
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select the Actuator
option under Search for dependencies
."
Note
Warnings or important notes appear like this.
Note
Tips and tricks appear like this.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows:
Getting ready
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
How to do it...
This section contains the steps required to follow the recipe.
How it works...
This section usually consists of a detailed explanation of what happened in the previous section.
There's more...
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email [email protected]
and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected]
.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected]
with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.