Download Complete (Ebook) Spring Boot 2.0 Projects: Build production-grade reactive applications and microservices with Spring Boot (English Edition) by Mohamed Shazin Sadakath ISBN 9781789136159, 1789136156 PDF for All Chapters
Download Complete (Ebook) Spring Boot 2.0 Projects: Build production-grade reactive applications and microservices with Spring Boot (English Edition) by Mohamed Shazin Sadakath ISBN 9781789136159, 1789136156 PDF for All Chapters
com
DOWLOAD EBOOK
ebooknice.com
BIRMINGHAM - MUMBAI
WOW! eBook
www.wowebook.org
Spring Boot 2.0 Projects
Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means, without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented.
However, the information contained in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy
of this information.
ISBN 978-1-78913-615-9
www.packtpub.com
WOW! eBook
www.wowebook.org
To my parents, for going through tough times in order to give me a good education
and manners, and to my wife, Nadhiya, for being my loving partner
throughout our life journey together.
WOW! eBook
www.wowebook.org
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as
well as industry leading tools to help you plan your personal development and advance
your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.PacktPub.com, and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
WOW! eBook
www.wowebook.org
Contributors
I would like to thank my loving wife, Nadhiya, for her help and encouragement while
writing this book. I would like to thank Gunith Devasurendra for the technical reviewing
of this book. I would also like to thank Sudharshan Selvenayagam and Manoj Senevirathne
for their assistance while writing this book.
WOW! eBook
www.wowebook.org
About the reviewers
Gunith Eranda Devasurendra is a senior software engineer with 10 years, professional
development experience, specializing in Java. Born in Sri Lanka, he became interested in
programming as he considers elegant programming and design to be an art form. Gunith
has a master's in computer science awarded by the University of Colombo. He is a speaker
and a trainer on technical topics including Spring, and Git. He is also an advocate of FOSS.
Gunith also helps out in the Stack Overflow community.
To Lord Buddha. To my parents for raising me and giving me an education so that I could
contribute to this book. To my wife, Vimanga, for her love and helping me on so many
levels so that I can contribute. To my son Sasen for his love. To Shazin, the author, for
nominating me and trusting me. To all my teachers and friends who taught me things.
Biharck Araújo has been working as a principal software architect and lead programmer
for the past 15 years. He is passionate about technology and academic research. He has
been working with JavaEE technology for web projects that demand high-security
standards in terms of information transmission for companies across different sectors. He
has extensive experience in activities regarding software architecture. He works in
bioinformatics using technology in life's favor.
WOW! eBook
www.wowebook.org
Table of Contents
Preface 1
Chapter 1: Introduction 6
Technical requirements 6
Generating Spring Boot Projects 7
Opening the generated project with IntelliJ 8
Opening the generated project with STS 10
Getting started with Spring Boot 12
Learning about Spring Boot 12
Anatomy of a Spring Boot application 13
Supporting the Spring Framework ecosystem in Spring Boot 15
Changes since Spring Boot 1.x 16
Registering a Spring Bean using ApplicationContextInitializer 17
Configuration property binding 18
New property binding API 19
Property origin 20
Tightened rules for governing relaxed property binding 20
Environment variables with indices 21
Direct binding of property type java.time.Duration in the ISO-8601 form 21
Custom endpoints for Spring Boot Actuator using annotations 21
Exposing a custom Spring Boot Actuator endpoint 22
Extending a custom endpoint with a specialized implementation for the web 23
Connecting to a custom endpoint using monitoring and management tools 23
Custom metrics using Micrometer 25
Custom health indicator 26
Using the HTTP/2 protocol 27
Securing applications with Spring Security 30
The next milestone 31
Migration 32
Using the correct JDK and JVM 32
Running on Java 9 32
Tackling JAXBException 32
Using the correct AspectJ version 33
Being aware of limitations on Apache Cassandra drivers 33
Being aware of issues with the Maven Surefire Plugin 33
Using the upgraded Spring Framework 5.0 33
Modified CORS support behavior 34
Removed packages, classes, and methods 34
Dropped support for frameworks 34
Using the updated configuration properties 35
Using the changed servlet-specific server properties 35
Using the modified template engine extension handling 35
WOW! eBook
www.wowebook.org
Table of Contents
[ ii ]
WOW! eBook
www.wowebook.org
Table of Contents
[ iii ]
WOW! eBook
www.wowebook.org
Table of Contents
[ iv ]
WOW! eBook
www.wowebook.org
Table of Contents
[v]
WOW! eBook
www.wowebook.org
Table of Contents
[ vi ]
WOW! eBook
www.wowebook.org
Table of Contents
[ vii ]
WOW! eBook
www.wowebook.org
Table of Contents
[ viii ]
WOW! eBook
www.wowebook.org
Preface
This book is about Spring Boot 2.0 hands-on development for beginners, intermediate, and
expert-level software developers. The purpose of this book is to increase the practical
knowledge of the readers by going through the practical uses of the features introduced in
Spring Boot 2.0.
This book covers vast topics with example applications so that it is much easier to grasp
and use in real-life projects.
This book expects the readers to have some level of understanding of software
development using Java.
Chapter 2, Building a Basic Web Application, begins with the practical side of developing a
basic web application using the Spring Boot 2.0 Framework. It also talks about Spring Data
JPA persistence, Spring Thymeleaf view, and Spring WebFlux controllers.
Chapter 3, Building a Simple Blog Management System, presents the practical side of
developing a simple blog management system using the Spring Boot 2.0 framework. It also
talks about Spring Data Elasticsearch for persistence, Apache FreeMarker view, and Spring
WebFlux controllers.
WOW! eBook
www.wowebook.org
Preface
Chapter 5, Building a Reactive Movie Rating API Using Kotlin, discusses reactive movie rating
API development using Kotlin programming language with Spring Boot 2.0. It talks about
Spring Data MongoDB persistence, Spring WebFlux controllers, and Spring Security
authentication and authorization.
Chapter 7, Building a Twitter Clone with Spring Boot, covers Angular application acting as a
client for Spring Boot 2.0 REST API. It also talks about Spring Data JPA persistence,
Angular 5 frontend, Spring Web Flux controllers, and Spring Security OAuth2
authentication and authorization.
[2]
WOW! eBook
www.wowebook.org
Preface
Once the file is downloaded, please make sure that you unzip or extract the folder using the
latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/
PacktPublishing/Spring-Boot-2.0-Projects-Fundamentals-of-Spring-Boot-2.0. 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!
[3]
WOW! eBook
www.wowebook.org
Preface
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: "An ApplicationStartedEvent will be sent right after the application context
is refreshed but before any command-line runners run. "
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 File | Open from the menu bar."
[4]
WOW! eBook
www.wowebook.org
Preface
Get in touch
Feedback from our readers is always welcome.
General feedback: Email feedback@packtpub.com 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 questions@packtpub.com.
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 copyright@packtpub.com 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!
[5]
WOW! eBook
www.wowebook.org
1
Introduction
This chapter will introduce the reader to Spring Boot and explain how it stands out from
other competing frameworks. It will begin by explaining how to get started developing
applications using Spring Boot. Also, it will explain about Spring Boot 1.x and the
improvements introduced in Spring Boot 2.0. Furthermore, it will walk through the most
noticeable features and/or improvements of Spring Boot 2.0. Continuing on, it will explain
progress with Spring Boot 2.0 and supply tips on migration from the older versions to
Spring Boot 2.0.
Technical requirements
Technical requirements for this chapter are as follows:
To install Java Development Kit (JDK) 8, it can be downloaded from its official
page at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-
downloads-2133151.html
To install Maven 3, download it from its official page at https://maven.apache.
org/download.cgi
WOW! eBook
www.wowebook.org
Introduction Chapter 1
This tool allows the selection of a Project type (Maven Project, Gradle Project),
programming language (Java, Groovy, Kotlin), Spring Boot version (2.0.*, 1.5.*), project
artifact group, artifact name, and project dependencies. After selecting the correct options,
click on Generate Project will download a ZIP file of the project.
[7]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
The ZIP file needs to be extracted first before being used. The extracted ZIP file will have
the following structure:
<Project Name>/
├── src/
├── pom.xml
├── mvnw
└── mvnw.bat
[8]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
3. Navigate to the location where the extracted project is and click on OK after
selecting the project, shown as follows:
[9]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
1. Open STS.
2. Select File | Open Projects from File System... from the menu bar, as shown in
the following screenshot:
[ 10 ]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
3. From the dialog box that launched, click on the Directory... button:
4. Navigate to the extracted project location and Click on OK after selecting the
project:
[ 11 ]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
[ 12 ]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
Flexible: A Spring Boot application will have most of its settings auto-configured
with default settings based on the dependencies available in the classpath of the
application. But the auto-configuration will step back whenever a custom
configuration is made. An example for this would be when a Spring Boot
application finds a MySQL JDBC driver in the classpath; it auto-
configures DataSource, which connects to the host localhost and port 3306, as
those will be the settings for a MySQL Server with a default installation.
Extensible: A Spring Boot application will have most core functionalities
implemented out of the box, but also has a lot of Service Provider Interfaces
(SPI), which are used by third-party developers to implement or modify
functionality. An example of this would be when a requirement arises for a
custom endpoint in Spring Boot Actuator; extending and overriding
the AbstractEndpoint.invoke method in a Spring Bean will expose it as a
new endpoint under Spring Boot Actuator.
Spring Boot does not do any code generation and does not require any XML files to be
configured in order to run. Spring Boot is ideal for on-premise and cloud-based
deployments with a quick boot-up time and a good memory footprint. The uniqueness of
Spring Boot comes from its ecosystem of Spring modules, which covers security, data
persistence, batch processing, and so on and from the highly active, competent community
of developers who keep on improving the Spring Boot Framework.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
[ 13 ]
WOW! eBook
www.wowebook.org
Introduction Chapter 1
</dependency>
...
</dependencies>
The minimum bootstrapping point of a Spring Boot application will be a class with a main
method that will be annotated with a @SpringBootApplication annotation along with
the main method body, which calls the SpringApplication.run static method, for which
a configuration class (a class with @Configuration
annotation—the @SpringBootApplication annotation transitively has one) needs to be
passed, along with a String array of arguments. The following code shows the minimum
bootstrapping point of a Spring Boot application:
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootIntroApplication {
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
System.out.println("Hello, World!");
};
}
}
By running the preceding class, a Spring Boot application can be provisioned and executed.
There are several ways to run a Spring Boot application; some of them are mentioned here:
[ 14 ]
WOW! eBook
www.wowebook.org
Exploring the Variety of Random
Documents with Different Content
tulevaisuuteni on turvattu, olen arvossapidetty, hyvin toimeentuleva
mies, jonka sopii mennä naimisiinkin!
— Sanoin jo, että tehdessäni tuon tekoni olin luullut olevani yksin,
mutta olinkin erehtynyt. Verhon tai huonekalujen varjossa oli istunut
joku — aurinko oli jo laskemaisillaan ja varjot eivät voineet olla niin
selväpiirteisiä — muuan paikananoja, joka odotti isäntääni ja joka oli
ohjattu tähän huoneeseen. Epäilemättä hän oli nähnyt tekoni, mutta
hän ei ollut puhunut mitään, ei ollenkaan tullut, esille, eikä kukaan
muu ollut nähnyt minun menevän tuohon huoneeseen tai tulevan
sieltä. Illan tultua etsittiin turhaan noita tuhatta frangia, jotka minä
jo olin toimittanut piilopaikkaan, ja niiden katoamisesta ei syytetty
minua, vaan tuota tuntematonta miestä, paikanhakijaa, jonka
kalpeus ja mielenjärkytys oli kiinnittänyt johtajan huomiota. Kaikki
kehittyi minun edukseni. Pidetyssä kuulustelussa ei kukaan
ajatellutkaan edes tiedustella minulta mitään, niin vieraaksi koko
asialle minua katsottiin. Mitä tuohon miesparkaan tulee, niin ei
hänellä ollut mitään todistetta syyttömyydestään, ei mitään
mahdollisuutta todistaa minun syyllisyyttäni, ja epäilemättä
puolustautuminen kävi hänelle tuiki vaikeaksi. Ilmeistä on, että hänet
olisi toimitettu vankilaan mitä pahimpana veijarina, jollei
liikkeenomistaja olisi katsonut edullisemmaksi jättää asiaa siksensä,
ettei siitä koituisi häpeää hänen kauppahuoneellensa. Muukalaiselle
syydettiin vasten silmiä kaikki halveksiminen, joka hänen katsottiin
ansaitsevan, ja hänet velvoitettiin lähtemään Pariisista… Minä olin
pelastettu. Siitä on nyt jo kulunut viisikolmatta vuotta, ja voisin
vannoa, etten ole näiden viidenkolmatta vuoden kuluessa tehnyt
ainoatakaan tekoa, jonka takia minun pitäisi punastua… jos nimittäin
punastuminen kuuluisi tapoihini. Olin saavuttanut mahdollisuuden
olla rehellinen ja tulin rehelliseksi. Nyt olen rikas ja olen jo aikoja
sitten lähettänyt tuolle kauppahuoneelle nimeäni ilmoittamatta
maksun muodossa nuo rahat, jotka sieltä… varastin. Nyt olen minä
suuressa arvossa pidetty henkilö, jos en olekaan liioin rakastettu.
Kaiken päälliseksi olen kuntani määri, ja on olemassa suuria
edellytyksiä, että ensi vaalien jälkeen herään jonakin aamuna
piirikuntani edustajana. Ajatelkaapas, missä olisinkaan tänään, jos
tuolla miehellä, jota syytettiin minun asemestani ja jota minä varoin
puhdistamasta noista syytöksistä, olisi ollut pienintäkään
mahdollisuutta kääntää syytökset minuun? Kohtalo ei suonut hänelle
mitään keinoa syöstä minua perikatoon… ja minä syöksin hänet.
Vaimosi
Gabrielle.
*****
— Niin juuri, hän on aivan liian rikas… ja liian ruma! En minä pidä
hänestä ollenkaan!
*****
— Entä Jaillou? Voisi vaikka lyödä vetoa, että hän ottaa Jailloun,
jahka tuo mies uskaltaa kosia!
Tultuaan myllylle hän kulki puutarhan läpi, missä Jean Fadet riisteli
kukkia muuria peittävästä köynnösruususta. Väliin joku
kiinnitarrautunut varsi sai koko säleistön huojumaan, ja liiaksi
auenneista kukista putoilivat terälehdet maahan valkoisen
perhosparven kaltaisina. Jean näytti nauttivan tuosta ihanan tuoksun
sulostuttamasta työstään. Hän oli täyttänyt sadolla jo kaksi koppaa,
kun emäntä Gervaise saapui, lausuen hänelle tervehdyksen.
Mutta tästä huolimatta oli Jean Fadet, kuten kaikki, jotka täyttävät
velvollisuutensa siitä kovaa ääntä pitämättä, toisten epäsuosiossa.
Vähitellen oli hän joutunut palvelijoiden silmätikuksi. Aterioitaessa oli
hän alituisesti heidän pilkkansa esineenä. Ruuat eivät muka
kelvanneet hänelle, hän söi kuin mikäkin herra, hänen suustaan oli
mahdoton saada esiin kahta lausetta peräkkäin.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com