Upload
others
View
14
Download
0
Embed Size (px)
Citation preview
Pro Spring Boot
Felipe Gutierrez
Pro Spring Boot
Felipe Gutierrez Albuquerque New Mexico, USA
ISBN-13 (pbk): 978-1-4842-1432-9 ISBN-13 (electronic): 978-1-4842-1431-2DOI 10.1007/978-1-4842-1431-2
Library of Congress Control Number: 2016941344
Copyright © 2016 by Felipe Gutierrez
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director: Welmoed Spahr Lead Editor: Steve AnglinTechnical Reviewer: Manuel Jordan EleraEditorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson,
Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Mark PowersCopy Editor: Kezia EndsleyCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit www.springeronline.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected] , or visit www.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales .
Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484214329 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.
Printed on acid-free paper
To my wife, Norma Castaneda.
v
Contents at a Glance
About the Author ................................................................................................... xiii
About the Technical Reviewer .................................................................................xv
Acknowledgments .................................................................................................xvii
■Chapter 1: Introduction to Spring Boot .................................................................. 1
■Chapter 2: Your First Spring Boot Application ....................................................... 9
■Chapter 3: Spring Boot Auto-Confi guration, Features, and More ........................ 43
■Chapter 4: Spring Boot CLI .................................................................................. 73
■Chapter 5: Spring with Spring Boot ..................................................................... 89
■Chapter 6: Testing with Spring Boot .................................................................. 107
■Chapter 7: Data Access with Spring Boot .......................................................... 121
■Chapter 8: Web Development with Spring Boot ................................................. 149
■Chapter 9: Security with Spring Boot ................................................................ 177
■Chapter 10: Messaging with Spring Boot .......................................................... 211
■Chapter 11: Spring Boot Actuator ..................................................................... 245
■Chapter 12: Deploying Spring Boot ................................................................... 283
■Chapter 13: Spring Boot in the Cloud ................................................................ 307
■Chapter 14: Extending Spring Boot Apps .......................................................... 335
■Appendix: Spring Boot 1.4.x .............................................................................. 357
Index ..................................................................................................................... 361
vii
Contents
About the Author ................................................................................................... xiii
About the Technical Reviewer .................................................................................xv
Acknowledgments .................................................................................................xvii
■Chapter 1: Introduction to Spring Boot .................................................................. 1
Spring Boot ....................................................................................................................... 1
Spring Applications ................................................................................................................................. 2
Spring Boot to the Rescue ...................................................................................................................... 3
Why Spring Boot? ................................................................................................................................... 6
Spring Boot Features .............................................................................................................................. 6
Summary .......................................................................................................................... 7
■Chapter 2: Your First Spring Boot Application ....................................................... 9
Installing Spring Boot CLI ................................................................................................. 9
UNIX OSs: Linux, OS X, and Solaris ......................................................................................................... 9
Windows OS.......................................................................................................................................... 11
Spring Boot with Maven and Gradle ............................................................................... 13
Using Maven ......................................................................................................................................... 13
Using Gradle ......................................................................................................................................... 14
Spring Boot Using External Tools.................................................................................... 16
Spring Boot Using the Spring Initializr .................................................................................................. 16
Using the Spring Initializr with UNIX cURL ............................................................................................ 18
Spring Boot Using Spring Tool Suite (STS) ............................................................................................ 19
■ CONTENTS
viii
Your First Spring Boot Application .................................................................................. 24
Spring Boot Journal .............................................................................................................................. 24
How Spring Boot Works ........................................................................................................................ 39
Summary ........................................................................................................................ 41
■Chapter 3: Spring Boot Auto-Confi guration, Features, and More ........................ 43
Auto-Confi guration ......................................................................................................... 43
Disabling a Specifi c Auto-Confi guration ............................................................................................... 45
@EnableAutoConfi guration and @Enable<Technology> Annotations ............................ 47
Spring Boot Features ...................................................................................................... 49
SpringApplication Class ........................................................................................................................ 51
SpringApplicationBuilder ...................................................................................................................... 56
Application Arguments ......................................................................................................................... 58
ApplicationRunner and CommandLineRunner ...................................................................................... 60
Application Confi guration ............................................................................................... 62
Confi guration Properties Examples ...................................................................................................... 63
Custom Properties Prefi x ...................................................................................................................... 69
Summary ........................................................................................................................ 72
■Chapter 4: Spring Boot CLI .................................................................................. 73
Spring Boot CLI ............................................................................................................... 73
The run Command ................................................................................................................................ 74
The test Command ............................................................................................................................... 76
The grab Command .............................................................................................................................. 78
The jar Command ................................................................................................................................. 79
The war Command ............................................................................................................................... 80
The install Command ............................................................................................................................ 81
The uninstall Command ........................................................................................................................ 81
The init Command ................................................................................................................................ 82
The shell Command .............................................................................................................................. 85
The help Command ............................................................................................................................... 86
Summary ........................................................................................................................ 87
■ CONTENTS
ix
■Chapter 5: Spring with Spring Boot ..................................................................... 89
Spring Web Applications ................................................................................................. 89
J2EE Web Applications ......................................................................................................................... 89
Spring MVC Applications....................................................................................................................... 93
Spring Boot Web Applications ............................................................................................................... 98
Using Spring with Spring Boot ..................................................................................... 101
XML with Spring Boot ......................................................................................................................... 101
Groovy Beans in Spring Boot .............................................................................................................. 102
Standalone Spring Apps vs. Spring Boot Apps .................................................................................. 103
Using Spring Technologies in Spring Boot .................................................................... 104
Summary ...................................................................................................................... 105
■Chapter 6: Testing with Spring Boot .................................................................. 107
Testing Spring Boot ...................................................................................................... 107
Web Testing .................................................................................................................. 109
Summary ...................................................................................................................... 120
■Chapter 7: Data Access with Spring Boot .......................................................... 121
SQL Databases ............................................................................................................. 121
Data Access Using the JDBC Template with Spring Boot ................................................................... 122
Data Access Using JPA with Spring Boot............................................................................................ 131
NoSQL Databases ......................................................................................................... 140
Summary ...................................................................................................................... 147
■Chapter 8: Web Development with Spring Boot ................................................. 149
Spring MVC ................................................................................................................... 149
Spring Boot Web Applications ...................................................................................... 150
Playing with the HAL Browser ...................................................................................... 171
Summary ...................................................................................................................... 175
■ CONTENTS
x
■Chapter 9: Security with Spring Boot ................................................................ 177
Simple Security for Spring Boot ................................................................................... 177
Security Using the application.properties File .................................................................................... 181
In-Memory Security ............................................................................................................................ 182
Security Using a Database .................................................................................................................. 183
Securing Resources ............................................................................................................................ 186
Spring Boot with OAuth2 .............................................................................................. 199
Summary ...................................................................................................................... 209
■Chapter 10: Messaging with Spring Boot .......................................................... 211
What Is Messaging? ..................................................................................................... 211
JMS with Spring Boot ................................................................................................... 211
A Simpler JMS Consumer ................................................................................................................... 217
Connect to Remote JMS Server .......................................................................................................... 221
RabbitMQ with Spring Boot .......................................................................................... 221
Installing RabbitMQ ............................................................................................................................ 221
RabbitMQ/AMQP: Exchanges, Bindings, and Queues .......................................................................... 221
Remote RabbitMQ ............................................................................................................................... 231
Redis Messaging with Spring Boot .............................................................................. 231
Installing Redis ................................................................................................................................... 231
Remote Redis ..................................................................................................................................... 237
WebSockets with Spring Boot ...................................................................................... 237
Summary ...................................................................................................................... 244
■Chapter 11: Spring Boot Actuator ..................................................................... 245
Spring Boot Actuator .................................................................................................... 245
/actuator ............................................................................................................................................. 248
/autoconfi g ......................................................................................................................................... 249
/beans ................................................................................................................................................. 250
/confi gprops ....................................................................................................................................... 251
/docs ................................................................................................................................................... 252
/dump ................................................................................................................................................. 253
■ CONTENTS
xi
/env ..................................................................................................................................................... 254
/fl yway ................................................................................................................................................ 255
/health ................................................................................................................................................ 261
/info .................................................................................................................................................... 262
/liquibase ............................................................................................................................................ 263
/logfi le................................................................................................................................................. 268
/metrics .............................................................................................................................................. 270
/mappings ........................................................................................................................................... 272
/shutdown ........................................................................................................................................... 273
/trace .................................................................................................................................................. 274
Sensitive Endpoints ...................................................................................................... 275
Changing the Endpoint ID ............................................................................................. 276
Actuator CORS Support ................................................................................................ 276
Changing the Management Endpoints Path ................................................................. 276
Using Spring Boot Actuator in a Non-Web Application ................................................. 277
Summary ...................................................................................................................... 281
■Chapter 12: Deploying Spring Boot ................................................................... 283
Setting Up the Spring Boot Journal App ....................................................................... 283
Creating the SSL Self-Signed Keystore .............................................................................................. 289
Testing SSL ................................................................................................................... 290
Creating Executable JARs ............................................................................................ 293
The Java Way ...................................................................................................................................... 294
The Spring Boot Way .......................................................................................................................... 294
Creating Executable and Deployable WARs .................................................................. 295
Deploying to a Tomcat-Based Server ................................................................................................. 298
Activating Profi les ............................................................................................................................... 299
Creating Spring Boot Apps as a Service ....................................................................... 301
Spring Boot Apps as Windows Service ............................................................................................... 302
Spring Boot with Docker .............................................................................................. 303
Summary ...................................................................................................................... 306
■ CONTENTS
xii
■Chapter 13: Spring Boot in the Cloud ................................................................ 307
The Cloud and Cloud-Native Architectures ................................................................... 307
Twelve-Factor Applications .......................................................................................... 308
Microservices ............................................................................................................... 309
Preparing the Spring Boot Journal App as Microservice .................................................................... 309
Cloud Foundry .............................................................................................................. 311
Cloud Foundry .................................................................................................................................... 312
Pivotal Cloud Foundry Features .......................................................................................................... 312
Cloud Foundry CLI - Command Line Interface .................................................................................... 313
Development Enviroment - PCFDev .................................................................................................... 313
Pivotal Cloud Foundry .................................................................................................. 322
Deploying to Pivotal Web Services ..................................................................................................... 325
Summary ...................................................................................................................... 333
■Chapter 14: Extending Spring Boot Apps .......................................................... 335
Custom Spring Boot Module ......................................................................................... 335
The spring-boot-journal Project ......................................................................................................... 335
The journal-spring-boot-starter Project ............................................................................................. 337
The journal-spring-boot-autoconfi gure Project .................................................................................. 339
Package and Install the Journal Project ............................................................................................. 346
The spring-boot-calendar Project ....................................................................................................... 348
Custom Health Indicator ............................................................................................... 353
Summary ...................................................................................................................... 356
■Appendix: Spring Boot 1.4.x .............................................................................. 357
Spring Boot 1.4.X Release Notes .................................................................................. 357
Upgrading from Spring Boot 1.3 ......................................................................................................... 357
New and Noteworthy .......................................................................................................................... 359
Index ..................................................................................................................... 361
xiii
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, during which time he developed programs for companies in multiple vertical industries, including government, retail, healthcare, education, and banking. He currently works as a principal technical instructor for Pivotal, specializing in Cloud Foundry, Spring Framework, Spring Cloud Native Applications, Groovy, and RabbitMQ, among other technologies. He has worked as a solutions architect for big companies like Nokia, Apple, Redbox, and Qualcomm, among others. He is also the author of the Apress title Introducing Spring Framework .
xv
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 2010 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 for Apress, including Pro Spring, 4th Edition (2014), Practical Spring LDAP (2013), Pro JPA 2, Second Edition (2013), and Pro Spring Security (2013).
Read his 13 detailed tutorials about many Spring technologies and contact him through his blog at http://www.manueljordanelera.blogspot.com or follow him on his Twitter account at @dr_pompeii .
xvii
Acknowledgments
I would like to express all my gratitude to the Apress team—to Steve Anglin for accepting my proposal; to Mark Powers for keeping me on track and for his patience with me; to Matthew Moodie 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. Thanks to my brother Edgar Gerardo Gutierrez. Special thanks to my girls who keep me on track: Norma, Nayely my “Flaca,” and Ximena my “Gallito”. I love you girls!
—Felipe Gutierrez