Pro Spring Boot 2: An Authoritative Guide To Building Microservices, Web and Enterprise Applications, and Best Practices 2nd Edition Felipe Gutierrez
Pro Spring Boot 2: An Authoritative Guide To Building Microservices, Web and Enterprise Applications, and Best Practices 2nd Edition Felipe Gutierrez
com
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/spring-boot-messaging-messaging-apis-
for-enterprise-and-integration-solutions-felipe-gutierrez/
textbookfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textbookfull.com
https://textbookfull.com/product/pro-spring-mvc-with-webflux-web-
development-in-spring-framework-5-and-spring-boot-2-2nd-edition-
marten-deinum/
textbookfull.com
Pro Spring MVC with WebFlux: Web Development in Spring
Framework 5 and Spring Boot 2 2nd Edition Marten Deinum
https://textbookfull.com/product/pro-spring-mvc-with-webflux-web-
development-in-spring-framework-5-and-spring-boot-2-2nd-edition-
marten-deinum-2/
textbookfull.com
https://textbookfull.com/product/pro-spring-security-securing-spring-
framework-5-and-boot-2-based-java-applications-2nd-edition-carlo-
scarioni/
textbookfull.com
Felipe Gutierrez
Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and
Enterprise Applications, and Best Practices
Felipe Gutierrez
Albuquerque, NM, USA
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
WebFlux������������������������������������������������������������������������������������������������������������������������������������ 184
WebClient���������������������������������������������������������������������������������������������������������������������������� 186
WebFlux and Spring Boot Auto-configuration��������������������������������������������������������������������������� 187
Using WebFlux with Spring Boot����������������������������������������������������������������������������������������� 188
Reactive Data���������������������������������������������������������������������������������������������������������������������������� 196
MongoDB Reactive Streams������������������������������������������������������������������������������������������������ 196
Summary���������������������������������������������������������������������������������������������������������������������������������� 206
viii
Table of Contents
ix
Table of Contents
/actuator/loggers/{name}���������������������������������������������������������������������������������������������������� 334
/actuator/metrics����������������������������������������������������������������������������������������������������������������� 334
/actuator/mappings������������������������������������������������������������������������������������������������������������� 336
/actuator/shutdown������������������������������������������������������������������������������������������������������������� 337
/actuator/httptrace�������������������������������������������������������������������������������������������������������������� 339
Changing the Endpoint ID���������������������������������������������������������������������������������������������������� 340
Actuator CORS Support������������������������������������������������������������������������������������������������������� 341
Changing the Management Endpoints Path������������������������������������������������������������������������ 341
Securing Endpoints������������������������������������������������������������������������������������������������������������� 342
Configuring Endpoints��������������������������������������������������������������������������������������������������������� 342
Implementing Custom Actuator Endpoints�������������������������������������������������������������������������������� 343
ToDo App with Custom Actuator Endpoints������������������������������������������������������������������������� 343
Spring Boot Actuator Health������������������������������������������������������������������������������������������������������ 353
ToDo App with Custom HealthIndicator������������������������������������������������������������������������������� 358
Spring Boot Actuator Metrics���������������������������������������������������������������������������������������������������� 363
ToDo App with Micrometer: Prometheus and Grafana��������������������������������������������������������� 363
General Stats for Spring Boot with Grafana������������������������������������������������������������������������ 378
Summary���������������������������������������������������������������������������������������������������������������������������������� 381
Chapter 11: Spring Integration and Spring Cloud Stream with Spring Boot������ 383
Spring Integration Primer���������������������������������������������������������������������������������������������������������� 384
Programming Spring Integration����������������������������������������������������������������������������������������� 386
Using XML���������������������������������������������������������������������������������������������������������������������������� 392
Using Annotations���������������������������������������������������������������������������������������������������������������� 395
Using JavaConfig����������������������������������������������������������������������������������������������������������������� 397
ToDo with File Integration���������������������������������������������������������������������������������������������������� 398
Spring Cloud Stream����������������������������������������������������������������������������������������������������������������� 405
Spring Cloud������������������������������������������������������������������������������������������������������������������������ 405
Spring Cloud Stream����������������������������������������������������������������������������������������������������������� 407
Spring Cloud Stream App Starters��������������������������������������������������������������������������������������� 430
Summary���������������������������������������������������������������������������������������������������������������������������������� 431
x
Table of Contents
xi
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 501
xii
About the Author
Felipe Gutierrez is a solutions software architect, with
bachelor’s and master’s degrees in computer science from
Instituto Tecnologico y de Estudios Superiores de Monterrey
Campus Ciudad de Mexico. Gutierrez has over 20 years of
IT experience and has developed programs for companies
in multiple vertical industries, such as government, retail,
healthcare, education, and banking. He is currently working
as a platform and solutions architect for Pivotal, specializing
in cloud foundry PAS and PKS, Spring Framework, Spring
Cloud Native Applications, Groovy, and RabbitMQ, among other technologies. He
has also worked as a solutions architect for big companies like Nokia, Apple, Redbox,
and Qualcomm. Gutierrez is the author of Spring Boot Messaging (Apress, 2017) and
Introducing Spring Framework (Apress, 2014).
xiii
About the Technical Reviewer
Manuel Jordan Elera is an autodidactic developer and
researcher who enjoys learning new technologies for his
own experiments and creating new integrations. Manuel
won the Springy Award Community Champion and Spring
Champion 2013. In his little free time, he reads the Bible and
composes music on his guitar. Manuel is known as
dr_pompeii. He has tech reviewed numerous books,
including Pro Spring, 4th Edition (Apress, 2014), Practical
Spring LDAP (Apress, 2013), Pro JPA 2, Second Edition
(Apress, 2013), and Pro Spring Security (Apress, 2013). You
can read his detailed tutorials about Spring technologies and contact him through his
blog at www.manueljordanelera.blogspot.com, and follow him on his Twitter account,
@dr_pompeii.
xv
Acknowledgments
I would like to express all my gratitude to the Apress team: Steve Anglin for accepting
my proposal, Mark Powers for keeping me on track and for his patience with me, and
the rest of the Apress team involved in this project. Thanks to everybody for making this
possible.
Thanks to my technical reviewer, Manuel Jordan, for all the details and effort in his
reviews, and the entire Spring Boot team for creating this amazing technology.
Thanks to my parents, Rocio Cruz and Felipe Gutierrez, for all their love and support;
to my brother, Edgar Gerardo Gutierrez, and my sister-in-law, Auristella Sanchez, and
specially to my girls, who also keep me on track—Norma, Laura “Lau”, Nayely my “Flaca”,
and Ximena my “Gallito”—I love you girls. And to my baby, Rodrigo!
—Felipe Gutierrez
xvii
CHAPTER 1
Spring Framework 5
Welcome to the first chapter of the book, where I give you an introduction to the Spring
Framework, a little bit of history, and how it has evolved since its inception. This chapter
is for developers that are new to the Spring Framework. If you are an experienced Spring
Framework developer, you can skip this chapter.
Maybe you are thinking, “I want to learn Spring Boot. Why do I need to know about
Spring Framework?” Well, let me tell you that Spring Boot is Spring. Spring Boot has a
different mechanism for running Spring applications; to understand how Spring Boot
really works and does its job, it is necessary to know more about the Spring Framework.
A Little History
The Spring Framework was created in 2003 by Rod Johnson, author of J2EE Development
without EJB (Wrox Publishing, 2004). The Spring Framework was the response to all the
complexity that the J2EE specifications had at that time. Today, it has improved, but you
need to have a whole infrastructure to run certain aspects of the J2EE ecosystem.
We can say Spring is a complementary technology to Java EE. The Spring Framework
integrates several technologies, such as Servlet API, WebSocket API, concurrency
utilities, JSON Binding API, bean validation, JPA, JMS, and JTA/JCA.
The Spring Framework supports the dependency injection and common annotation
specifications that make development easier.
This chapter shows that the Spring Framework version 5.x requires a Java EE 7 level
(Servlet 3.1+ and JPA 2.1) as the minimum. Spring still works with Tomcat 8 and 9,
WebSphere 8, and JBoss EAP 7. Also, I show you the new addition to the Spring
Framework 5— reactive support!
Nowadays, Spring is one of the most used and recognized frameworks in the Java
community, not only because it works, but because it continues to innovate with other
amazing projects, including Spring Boot, Spring Security, Spring Data, Spring Cloud,
Spring Batch, and Spring Integration, among others.
1
© Felipe Gutierrez 2019
F. Gutierrez, Pro Spring Boot 2, https://doi.org/10.1007/978-1-4842-3676-5_1
Chapter 1 Spring Framework 5
• Provide choice at every level. Spring lets you defer design decisions as
late as possible. For example, you can switch persistence providers
through configuration without changing your code. The same is true
for many other infrastructure concerns and integration with third-
party APIs. And you will see, this even happens when you deploy
your application to the cloud.
• Care about API design. The Spring team puts a lot of thought and
time into making APIs that are intuitive and that hold up across many
versions and many years.
• Set high standards for code quality. The Spring Framework puts a
strong emphasis on meaningful, current, and accurate Javadocs. It is
one of very few projects that can claim clean code structure with no
circular dependencies between packages.
So, what do you need to run a Spring application? Spring works with Plain Old
Java Objects (POJOs), making it easy to extend. Spring is not invasive and makes your
application enterprise ready; but you need to help Spring by adding a configuration to
wire up all dependencies and inject what’s needed to create Spring beans to execute your
application (see Figure 1-1).
2
Chapter 1 Spring Framework 5
Figure 1-1 shows the Spring context that creates all the Spring beans—thanks to the
configuration that references your classes, which makes your application run. You find
out more in the next sections, in which you create a complete REST API app.
Spring Framework 5
Spring makes it easy to create Java enterprise applications because it provides everything
that a developer needs to embrace the Java language in an enterprise environment. It
offers excellent support of Groovy and Kotlin as alternative languages on the JVM (Java
virtual machine).
Spring Framework 5 requires JDK 8+ and provides out-of-the-box-support for
Java Development Kit (JDK) 9, 10, and 11. The Spring team has the same long-term
maintenance support for 11 and version 17, which correlates with the JDK team. This
new version came out in 2017 with a new way to do functional programming with
Reactive Streams.
Spring Web MVC was built to serve the Servlet API and Servlet containers. This was
OK until there was more demand for services, which detected a particular problem:
there was some blocking on each request; and with high demand, it was necessary to
do something else. The result: the reactive stack, a web framework. The Spring WebFlux
module was introduced in version 5, with a fully non-blocking stack that supports
Reactive Streams back pressure and runs on servers such as Netty, Undertow, and
Servlet 3.1+ containers. This was part of the answer for a non-blocking stack that handles
concurrency with a small number of threads that can scale with less hardware.
3
Chapter 1 Spring Framework 5
The WebFlux module depends on another Spring project: Project Reactor. Reactor
is the reactive library of choice for Spring WebFlux. It provides the Mono and Flux API
types to work on data sequences of 0..1 and 0..N through a rich set of operators aligned
with the ReactiveX vocabulary of operators. Reactor is a Reactive Streams library, and
therefore, all of its operators support non-blocking back pressure. Reactor has a strong
focus on server-side Java. It is developed in close collaboration with Spring.
I don’t want to get into much of the Spring features because I can show them with
a simple web application. What do you think? All of these cool WebFlux features are
reviewed in its own chapter.
This command is generating the basic template and structure for web applications.
Normally, it generates the webapp and resources folders but not the java folder, which
you need to create manually.
4
Chapter 1 Spring Framework 5
todo
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── WEB-INF
│ └── web.xml
└── index.jsp
You can import the code in your favorite IDE; this will make it easier to identify any
problems.
A
dding Dependencies
Open pom.xml and replace all the content with Listing 1-1.
<properties>
5
Chapter 1 Spring Framework 5
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
6
Chapter 1 Spring Framework 5
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate-jpa.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
7
Chapter 1 Spring Framework 5
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${bootstrap.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>
<finalName>todo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Listing 1-1 shows the pom.xml file and all the dependencies that you need to create a
simple Spring web app.
9
Chapter 1 Spring Framework 5
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
It is necessary to set up the DispatcherServlet, which is the main entry point for
any Spring web app. This class wires up everything based on the context configuration.
As you can see, it is a very trivial configuration.
Next, let’s configure the Spring context by creating a dispatcherServlet-servlet.
xml file. There is a naming convention; if the servlet is named todo in the web.xml file,
then the Spring context file should be named todo-servlet.xml. In this case, the servlet
was named dispatcherServlet, so it looks for a dispatcherServlet-servlet.xml file
(see Listing 1-3).
10
Chapter 1 Spring Framework 5
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/mvc http://www.
springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.
springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.
springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.
springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.
springframework.org/schema/tx/spring-tx-4.3.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.
MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jsonMapper"/>
</bean>
<bean class="org.springframework.http.converter.xml.
MappingJackson2XmlHttpMessageConverter">
<property name="objectMapper" ref="xmlMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
11
Chapter 1 Spring Framework 5
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.
LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean
class="org.springframework.web.servlet.view.
InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
12
Chapter 1 Spring Framework 5
</beans>
Listing 1-3 shows the Spring web configuration. Take a look at all the XML
namespaces it uses. This can be helpful because if you use an IDE with code completion,
it gives you the components and their attributes for every entry. Let’s analyze it.
• jsonMapper and xmlMapper beans. The classes are Spring beans that
help format the data and create the right mapper.
13
Chapter 1 Spring Framework 5
As you can see, this part requires a little bit of knowledge on how to wire up Spring.
If you want to understand more, I recommend several books from the Apress, including
Pro Spring 5, by I. Cosmina, et al.
I want to show you what you need to do to run a simpler REST API; and believe me,
if you think that this is too much, try to do the same with Java EE with all the features this
app has (MVC, JPA, SQL initialization, JSP, transactions).
14
Other documents randomly have
different content
ja katseli eteenpäin. Sitten hän kääntyi heiluttaen hattuaan.
Kuormahevoset pysähtyivät yhteen joukkoon. Dale viittasi tyttöjä
seuraamaan ja ratsasti Royn hevosen rinnalle. Helen huomasi tämän
paikan olevan muutaman poikkipäin kulkevan solan reunalla. Dalekin
laskeutui maahan irroittamatta pyssyään satulasta riippuvasta
tupesta ja lähestyi Royta.
"Ne ovat alempana meitä", kuiskasi Dale. "Ja sinä ammut varmasti
yli."
"Sanoinhan sen sinulle jo. Näin, että luotisi sattui mäntyyn puoli
jalkaa sen lautasten yläpuolelle. Koeta jälleen ja tähtää sen
jalkoihin."
Joki oli tässä kohden leveä, mutta ei syvä, vaikka näytti sellaiselta.
Bo karjaisi.
Kun puoli tusinaa rinteitä oli sivuutettu sillä tavoin, olivat Helenin
voimat lopussa eikä hän voinut enää juuri hengittääkään. Häntä
pyörrytti eikä hän voinut saada tarpeekseen ilmaa. Hänen jalkansa
tuntuivat lyijynraskailta ja ratsastuskaapu tuntui oikealta kuormalta.
Hänen oli pakko pysähtyä satoja kertoja lämmön, hiostumisen ja
hengästymisen vuoksi. Hän oli aina ennen ollut hyvä kävelijä ja
kiipeilijä, ja täällä vaihteeksi tähän pitkään ratsastusmatkaan oli hän
iloinen saadessaan kävellä. Mutta hän voi vain vetää jalkoja
perässään. Sitten kun hänen nenästään alkoi vuotaa verta, ymmärsi
hän, että syynä tähän kaikkeen oli vain tämän korkealla olevan
seudun ohut ilma. Hänen sydäntään ei kumminkaan pakottanut,
vaikka hän tunsikin jonkunlaista painoa rinnassaan.
Helen irroitti suitset käsistään juuri kun suuri vyöry aikoi tempaista
Rangerin mukaansa. Se korskahti hurjasti ja nousten takajaloilleen
pääsi se lujalle maalle mahtavalla hypyllä. Helen hautautui polviaan
myöten, mutta vapautettuaan itsensä ryömi hän turvalliseen
paikkaan ja lepäsi, ennenkuin läksi jatkamaan kiipeämistä.
"Tuo on paha paikka", sanoi Roy, kun Helen lopulta yhtyi häneen
ja
Bohun rinteen laella.
"Minä ja Milt eksymme joka päivä", sanoi Roy. "Ette suinkaan luule
kenenkään miehen tuntevan perinpohjin tätä laajaa seutua?
Tällainen eksyminen ei merkitse mitään."
"Olisi ollut parempi, etten olisi sanonut sitä teille niin suoraan",
sanoi Roy katuvasti. "Älkää olko pahoillanne nyt. Minun on vain
saatava Old Baldyn huippu näkyviin ja silloin olen taasen varma
suunnasta. No tulkaahan nyt."
"Lyön vaikka vetoa, että jos hän vain tuntee Dalen hyvin, ei hän
ole hyvinkään huolissaan."
Kun hän heräsi oli kaikki valoisaa. Aurinko paistoi melkein suoraan
ylhäältä. Hän hämmästyi. Bo nukkui vielä sikeästi, hänen kasvonsa
olivat punaiset, otsassa oli hikipisaroita ja kastanjanruskea tukka oli
aivan kostea. Helen heitti huopapeitteet yltään ja sitten rohkaisten
mieltään, sillä hänestä tuntui kuin hänen selkänsä olisi ollut poikki,
koetti hän nousta istualleen, mutta ei onnistunut. Hänen henkensä
olisi kyllä ollut altis, mutta lihakset kieltäytyivät tottelemasta. Hänen
oli tehtävä toinen suonenvedontapainen voimakas yritys. Hän ryhtyi
siihen suljetuin silmin ja onnistuttuaan hän istui siinä vapisten.
Hänen tekemänsä liikkeet huopapeitteiden alla herättivät Bon, joka
tuijotti nyt hämmästynein sinisin silmin aurinkoon.
"Halloo, Nell, onko minunkin noustava?" kysyi hän unisesti.
"Nousta tietysti!"
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.
textbookfull.com