0% found this document useful (0 votes)
107 views

2021 Bookmatter SpringBootWithReactAndAWS

Spring Boot

Uploaded by

Samuel Akohoue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

2021 Bookmatter SpringBootWithReactAndAWS

Spring Boot

Uploaded by

Samuel Akohoue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Spring Boot with

React and AWS


Learn to Deploy a
Full Stack Spring Boot React
Application to AWS

Ravi Kant Soni
Namrata Soni
Spring Boot with React and AWS: Learn to Deploy a Full Stack Spring Boot
React Application to AWS
Ravi Kant Soni Namrata Soni
s/o – Late. Ras Bihari Prasad, Sri Niwash, d/o – Late. Ras Bihari Prasad, Sri Niwash,
Lashkariganj, Sasaram, Bihar, India Lashkariganj, Sasaram, Bihar, India

ISBN-13 (pbk): 978-1-4842-7391-3 ISBN-13 (electronic): 978-1-4842-7392-0


https://doi.org/10.1007/978-1-4842-7392-0

Copyright © 2021 by Ravi Kant Soni and Namrata Soni


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.
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, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Spandana Chatterjee
Development Editor: James Markham
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Pixabay
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1
New York Plaza, Suite 4600, New York, NY 10004-1562, USA. 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]; for reprint,
paperback, or audio rights, please e-mail [email protected].
Apress titles 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 Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
9781484273913. For more detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
To my beloved father,
the late Ras Bihari Prasad
We miss you and love you, Papa. A strong and gentle
soul who taught us to trust God and to believe in ourselves
and our dreams.

To my beloved mother,
Smt. Manorma Devi
We love you, Maa. We could never have completed this
book without your true love, warmest support, and
constant encouragement.
Table of Contents
About the Authors��������������������������������������������������������������������������������ix

About the Technical Reviewer�������������������������������������������������������������xi


Acknowledgments�����������������������������������������������������������������������������xiii

Chapter 1: A
 n Introduction to Amazon Web Services (AWS)����������������1
Introduction to Amazon Web Services������������������������������������������������������������������2
AWS Key Services�������������������������������������������������������������������������������������������������4
Elastic Cloud Compute (EC2)���������������������������������������������������������������������������5
Elastic Beanstalk���������������������������������������������������������������������������������������������7
Relational Database Service (RDS)������������������������������������������������������������������8
Route 53����������������������������������������������������������������������������������������������������������8
Use Case: AWS Application Architecture���������������������������������������������������������������9
Create a Free AWS Account for Developer������������������������������������������������������������9
Explore and Create an AWS Elastic Beanstalk Server�����������������������������������������19
Create a HelloWorld JSP Application, Build WAR with Maven,
and Upload WAR to Elastic Beanstalk�����������������������������������������������������������������26
Create a HelloWorld JSP Application�������������������������������������������������������������26
Package a WAR File Using Maven�����������������������������������������������������������������32
Upload WAR to Elastic Beanstalk�������������������������������������������������������������������34
Summary������������������������������������������������������������������������������������������������������������40

v
Table of Contents

Chapter 2: Deploy a Spring Boot Application as a


REST API in AWS���������������������������������������������������������������������������������41
Build a Spring Boot Application as a REST API����������������������������������������������������42
Introduction to REST��������������������������������������������������������������������������������������42
System Requirements�����������������������������������������������������������������������������������44
Create Spring Boot Application Using Spring Tool Suite��������������������������������45
A Walk-Through���������������������������������������������������������������������������������������������48
Run a Spring Boot Application in STS������������������������������������������������������������53
Add Swagger UI to a Spring Boot Application�����������������������������������������������������56
Introduction to Swagger 2�����������������������������������������������������������������������������56
Add Dependency in a Maven POM�����������������������������������������������������������������57
Configure Swagger 2 into a Project���������������������������������������������������������������57
Configuration Verification������������������������������������������������������������������������������59
Swagger UI����������������������������������������������������������������������������������������������������60
Configure the Server Port for a Spring Boot Project�������������������������������������������61
Build a JAR for a Spring Boot Application�����������������������������������������������������������63
Deploy a Spring Boot Application in AWS Elastic Beanstalk�������������������������������67
Test a Spring Boot Application as a REST API in the Cloud���������������������������������73
Explore Logs from Elastic Beanstalk�������������������������������������������������������������������74
Summary������������������������������������������������������������������������������������������������������������75

Chapter 3: D
 eploy MySQL as a Database in AWS with RDS����������������77
Introduction to Amazon RDS (Amazon Relational Database Service)�����������������78
Create an Instance of the RDS Database in AWS������������������������������������������������78
Configure Amazon RDS���������������������������������������������������������������������������������������86
Step 1. Configure Security for Inbound Connection Rules�����������������������������88
Step 2. Test an Amazon RDS Database Instance Connection with
MySQL Workbench����������������������������������������������������������������������������������������91

vi
Table of Contents

Create a Table Inside an RDS Database Instance������������������������������������������������96


Summary����������������������������������������������������������������������������������������������������������102

Chapter 4: Deploy a Spring Boot Application Talking to


MySQL in AWS����������������������������������������������������������������������������������103
Create Spring Boot UserRegistrationApp Talking to MySQL Database��������������103
Maven Dependency in pom.xml������������������������������������������������������������������105
Project Lombok�������������������������������������������������������������������������������������������108
Application Properties���������������������������������������������������������������������������������111
Domain Implementation: UserDTO Entity Class�������������������������������������������112
Repository Implementation: UserJpaRepository�����������������������������������������114
Service Implementation: UserService���������������������������������������������������������116
REST Controller Implementation: UserRegistrationController����������������������117
Run and Test UserRegistrationApp Locally��������������������������������������������������������121
Retrieve All Users: /api/users����������������������������������������������������������������������122
Retrieve an Individual User: /api/user/id/{id}�����������������������������������������������123
Create a New User: /api/user/save��������������������������������������������������������������124
Delete an Existing User: /api/user/delete/id/{id}������������������������������������������126
Swagger UI: API Documentation������������������������������������������������������������������126
Build a JAR for a Spring Boot Application���������������������������������������������������������128
Deploy the UserRegistrationApp Spring Boot Application in
AWS Elastic Beanstalk��������������������������������������������������������������������������������������129
Test Deployed REST API in AWS Using Swagger UI�������������������������������������������135
List All Users: /api/users������������������������������������������������������������������������������137
Create New Users: /api/users����������������������������������������������������������������������139
Summary����������������������������������������������������������������������������������������������������������141

vii
Table of Contents

Chapter 5: Deploy a Full Stack Spring Boot React Application


in AWS and S3����������������������������������������������������������������������������������143
Develop and Run React as a Front-End Application������������������������������������������145
Introducing React as a Front-end Framework���������������������������������������������145
Set up a Development Environment������������������������������������������������������������149
Cross-Origin Resource Sharing (CORS) Error����������������������������������������������150
Developing React Front-End Application with create-react-app�����������������151
Build React Code as a Front-end Application for AWS��������������������������������������180
Verify the AWS Elastic Beanstalk Environment Is Up�����������������������������������180
Update BaseURL in a React App with an AWS Elastic Beanstalk
Environment URL�����������������������������������������������������������������������������������������181
Build React Code for AWS Deployment��������������������������������������������������������183
Deploy a React Front-End to AWS S3: Hosting a Static Website�����������������������185
Introduction to S3: Simple Storage Service in AWS�������������������������������������185
Create a Bucket�������������������������������������������������������������������������������������������188
Verify the Successful Deployment of a React Front-end Application:
Resolve a 404 Error�������������������������������������������������������������������������������������198
Summary����������������������������������������������������������������������������������������������������������200

Appendix A: Install MySQL Workbench on Windows 10�������������������201


Step 1. Download Workbench���������������������������������������������������������������������������201
Step 2. Install Workbench���������������������������������������������������������������������������������204

Appendix B: AWS Command-Line Interface (CLI)������������������������������213


Step 1. Download and Install the AWS CLI on a Windows Operating System���214
Step 2. Create an Access Key���������������������������������������������������������������������������214
Configure AWS CLI��������������������������������������������������������������������������������������������217
Example Commands That Work with S3�����������������������������������������������������������217

Index�������������������������������������������������������������������������������������������������219

viii
About the Authors
Ravi Kant Soni is a principal full stack
engineer with more than 11 years of IT
experience. He is also an AWS Certified
Solutions Architect. Ravi has experience
in software development, software
design, systems architecture, application
programming, and automation testing. He has
a bachelor’s degree in Information Science and
Engineering from Reva University, Bangalore;
and schooling from Bal Vikash Vidyalaya, Sasaram, and Bihar (India).
He is the author of Build Microservices with Spring Cloud and Spring
Boot (codered eccouncil, 2021), Full Stack AngularJS for Java Developers
(Apress, 2018), Spring: Developing Java Applications for the Enterprise
(Packt, 2017), and Learning Spring Application Development (Packt,
2015). He is also an esteemed member of the Board of Studies at the REVA
University School of Computing and Information Technology in Bangalore.
Contact Ravi at ­www.linkedin.com/in/november03ravikantsoni/. 

ix
About the Authors

Namrata Soni is a self-taught web application


developer with a passion for beautiful and
interactive UIs. She has a degree in computer
science from Sagar Institute of Science &
Technology, Bhopal; and schooling from Bal
Vikash Vidyalaya, Sasaram, and Bihar (India).
She loves clean and well-tested code, is a
big fan of open source, and enjoys learning
something new. Currently, she is working
with React and Node.js to craft modern JavaScript applications. Contact
Namrata at w­ ww.linkedin.com/in/september-­6-­namrata-­soni/.  

x
About the Technical Reviewer
Karunesh Chandra Tiwari is an IT professional
with ten years of experience and has worked
across distinct technologies and domains. He is
a technologist and speaker and loves to provide
his views on articles and blogs.
Karunesh is a BTech IT graduate from
Anna University. He worked as a full stack
developer for the first half of his career and
currently works with BPM and CRM tools
and cloud-related technologies, including
developing and working with applications for
some of the world’s leading banks. He is a very focused and determined
person and loves to learn, work in new technologies. He loves to mentor
people both from a professional and a personal perspective.
Karunesh enjoys working with new technologies and loves to
mentor people. Check out his LinkedIn profile at www.linkedin.com/in/
karunesh-­chandra-­tiwari-­20b9a82a/.

xi
Acknowledgments
Writing a technical book involves fathomless research, review, and
support. I wrote this book, but it wouldn’t have been possible without the
love and support of many people. I truly want to thank everyone listed
here, from the deep bottom of my heart!
First and foremost, I need to express gratitude toward Michael Gorriz,
Group Chief Information Officer, Standard Chartered Bank, for inspiring
me and giving me the confidence to write this book when I anticipated my
career break. All I can offer in return is a heartfelt thank you!
I want to thank my colleagues at Standard Chartered Bank. I learn
something new every day and enjoy a camaraderie I've never felt in any
company before. I am fortunate enough to work with such an experienced
team that helped me enhance my skills. My gratitude goes to Anshu
Sharma Raja, CIO, Consumer Private Business Banking at Standard
Chartered Bank, and Dr. Ashish Chandra, Location Head- aXess Labs
(Banking Innovation) at Standard Chartered Bank; for their guidance and
strong support.
I want to thank the Apress publishing team for the utmost
professionalism. The one individual who has been the roof of this shelter
is Divya Modi, coordinating editor, for supporting me in the writing of this
book. Also, I would like to express my special gratitude to James Markham,
development editor, whose vision, commitment, and persistent efforts
made publishing this book efficient.
My heartfelt thanks go to the technical reviewer, Karunesh Chandra
Tiwari, for his valuable input.

xiii
Acknowledgments

My deepest gratitude and appreciation go to my dear friend Awanish


Kumar, IAS – Deputy Commissioner, Delhi; for the intellectual stimulus
from time to time, which helped me approach the book from a unique
perspective.
Thanks to my dearest friend, Dr. Meena Soni (Incharge Medical
Officer, Surajpur - Basdei, and Chhattisgarh), for invariably motivating,
encouraging, and giving me positive thoughts that worked as fuel to carry on.
Without my families’ love, support, and understanding, this book
would have remained a virtual commodity. My profound thanks to my
beloved mother, Smt. Manorma Devi, for her love and support, which
encourages my knowledge to come out on paper to ignite the imagination
of others.
My special thanks go to a man who has been a rock of stability
throughout my life and whose loving spirit sustains me still—my uncle
Shri. Arun Kumar Soni for the great inspiration he has given me to achieve
all success in life. Thanks also to my brothers, Shashi Kant and Shree Kant,
and all my family members who have loved me.
My special thanks to my co-author and sister, Namrata Soni, for
agreeing to co-author this book and helping me write Chapter 5, which
discusses React and AWS S3. I’m still amazed that she agreed to get
involved with this book, considering how enormously busy she is.
Namrata, thank you!
I want to thank the goddess Maa Tara Chandi, Sasaram, Bihar, India;
for giving me to such an extent.
Finally, this book is based on the innovative work of many people in
our industry who have become my idols. I am thankful to everyone who
supported me in one way or another in writing this book.
Welcome to Spring Boot with React and AWS.

—Ravi Kant Soni

xiv

You might also like