PDF Pro RESTful APIs With Micronaut: Build Java-Based Microservices With REST, JSON, and XML, 2nd Edition Sanjay Patni Download
PDF Pro RESTful APIs With Micronaut: Build Java-Based Microservices With REST, JSON, and XML, 2nd Edition Sanjay Patni Download
https://ebookmass.com
https://ebookmass.com/product/pro-restful-apis-
with-micronaut-build-java-based-microservices-
with-rest-json-and-xml-2nd-edition-sanjay-patni/
https://ebookmass.com/product/coding-clean-reliable-and-safe-rest-
apis-with-asp-net-core-8-1st-edition-anthony-giretti/
testbankdeal.com
Sanjay Patni
Pro RESTful APIs with Micronaut: Build Java-Based Microservices with
REST, JSON, and XML
Sanjay Patni
Santa Clara, CA, USA
Introduction����������������������������������������������������������������������������������������xv
v
Table of Contents
Chapter 2: Micronaut��������������������������������������������������������������������������17
Abstract���������������������������������������������������������������������������������������������������������������17
Comparison of Micronaut with Spring Boot��������������������������������������������������������18
Ease of Installation����������������������������������������������������������������������������������������18
Natively Cloud Enabled����������������������������������������������������������������������������������19
Serverless Functions�������������������������������������������������������������������������������������19
Application Configuration������������������������������������������������������������������������������20
Messaging System Support���������������������������������������������������������������������������21
Security���������������������������������������������������������������������������������������������������������22
Caching����������������������������������������������������������������������������������������������������������23
Management and Monitoring������������������������������������������������������������������������23
API Portfolio��������������������������������������������������������������������������������������������������������24
Online Flight��������������������������������������������������������������������������������������������������24
Message��������������������������������������������������������������������������������������������������������25
Software��������������������������������������������������������������������������������������������������������������26
Micronaut������������������������������������������������������������������������������������������������������26
JDK 11�����������������������������������������������������������������������������������������������������������27
POSTMAN������������������������������������������������������������������������������������������������������27
CURL��������������������������������������������������������������������������������������������������������������27
IDE�����������������������������������������������������������������������������������������������������������������27
Maven������������������������������������������������������������������������������������������������������������28
Setting Up an IDE������������������������������������������������������������������������������������������������28
Configuring Visual Studio Code���������������������������������������������������������������������29
Summary������������������������������������������������������������������������������������������������������������30
vi
Table of Contents
vii
Table of Contents
Error Code������������������������������������������������������������������������������������������������������65
Versioning������������������������������������������������������������������������������������������������������66
Partial Response��������������������������������������������������������������������������������������������67
Pagination������������������������������������������������������������������������������������������������������67
Multiple Formats�������������������������������������������������������������������������������������������68
API Façade�����������������������������������������������������������������������������������������������������68
API Solution Architecture������������������������������������������������������������������������������������69
Mobile Solutions��������������������������������������������������������������������������������������������70
Cloud Solutions����������������������������������������������������������������������������������������������70
Web Solutions������������������������������������������������������������������������������������������������70
Integration Solutions�������������������������������������������������������������������������������������71
Multichannel Solutions����������������������������������������������������������������������������������71
Smart TV Solutions����������������������������������������������������������������������������������������71
Internet of Things������������������������������������������������������������������������������������������71
Stakeholders in API Solutions�����������������������������������������������������������������������������72
API Providers�������������������������������������������������������������������������������������������������72
API Consumers����������������������������������������������������������������������������������������������72
End Users������������������������������������������������������������������������������������������������������72
API Modeling�������������������������������������������������������������������������������������������������������72
Summary������������������������������������������������������������������������������������������������������������80
viii
Table of Contents
Longevity�������������������������������������������������������������������������������������������������������83
How Do We Enforce These Requirements—Governance?����������������������������83
Consistency���������������������������������������������������������������������������������������������������84
Reuse�������������������������������������������������������������������������������������������������������������84
Customization������������������������������������������������������������������������������������������������84
Discoverability�����������������������������������������������������������������������������������������������84
Change Management������������������������������������������������������������������������������������85
API Framework����������������������������������������������������������������������������������������������������85
Process APIs: Services Layer�������������������������������������������������������������������������86
System APIs: Data Access Object������������������������������������������������������������������87
Experience APIs: API Façade��������������������������������������������������������������������������87
Services Layer Implementation���������������������������������������������������������������������87
Summary������������������������������������������������������������������������������������������������������������95
ix
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Table of Contents
Index�������������������������������������������������������������������������������������������������135
x
About the Author
Sanjay Patni is a results-focused technologist
with extensive experience in aligning
innovative technology solutions with business
needs to optimize manual steps in the
business processes and improving operational
efficiency.
At Oracle, he has worked with the Fusion
Apps Product development team, where he
has identified opportunities for automation
of programs related to Fusion Apps codeline management. This involved
delivery of GA releases for patching, as well as codelines for ongoing
demo, development, and testing. He conceptualized and developed self-
service UX for codeline requests and auditing, reducing manual steps by
80%. He also rolled out 12 sprints of codeline creation, automating about
100+ manual steps involving integration with other subsystems using
technologies like automation workflow and RESTful APIs.
Prior to joining Oracle, he spent 15+ years in the software industry,
defining and delivering key initiatives across different industry sectors.
His responsibilities included innovation, requirement, analysis, technical
architecture, design, and agile software development of web-based
enterprise products and solutions. He pioneered innovative usage of
Java in building business applications and received an award from Sun
Microsystems. This helped improve feedback for Java APIs for Enterprise
in building business application software using Java. He has diverse
experience in Application Architecture to include UX, Distributed Systems,
Cloud and DevOps.
xi
About the Author
xii
About the Technical Reviewer
Massimo Nardone has more than 22 years
of experience in security, web and mobile
development, cloud, and IT architecture. His
true IT passions are security and Android.
He has been programming and teaching
how to program with Android, Perl, PHP, Java,
VB, Python, C/C++, and MySQL for more than
20 years.
He holds a master of science degree in
computing science from the University of
Salerno, Italy.
He has worked as a project manager, software engineer, research
engineer, chief security architect, information security manager, PCI/
SCADA auditor, and senior lead IT security/cloud/SCADA architect for
many years.
His technical skills include security, Android, cloud, Java, MySQL,
Drupal, Cobol, Perl, web and mobile development, MongoDB, D3, Joomla,
Couchbase, C/C++, WebGL, Python, Pro Rails, Django CMS, Jekyll,
Scratch, etc.
He currently works as Chief Information Security Officer (CISO) for
Cargotec Oyj.
He worked as visiting lecturer and supervisor for exercises at the
Networking Laboratory of the Helsinki University of Technology (Aalto
University). He holds four international patents (PKI, SIP, SAML, and
Proxy areas).
Massimo has reviewed more than 40 IT books for different publishing
companies, and he is the coauthor of Pro Android Games (Apress, 2015).
xiii
Introduction
Databases, websites, and business applications need to exchange data.
This is accomplished by defining standard data formats such as Extensible
Markup Language (XML) or JavaScript Object Notation (JSON), as
well as transfer protocols or web services such as the Simple Object
Access Protocol (SOAP) or the more popular Representational State
Transfer (REST). Developers often have to design their own Application
Programming Interfaces (APIs) to make applications work while
integrating specific business logic around operating systems or servers.
This book introduces these concepts with a focus on the RESTful APIs.
This book introduces the data exchange mechanism and common data
formats. For web exchange, you will learn the HTTP protocol, including
how to use XML. This book compares SOAP and REST and then covers
the concepts of stateless transfer. It introduces software API design and
best design practices. The second half of the book focuses on RESTful APIs
design and implementations that follow the Micronaut and Java API for
RESTful Web Services. You will learn how to build and consume Micronaut
services using JSON and XML and integrate RESTful APIs with different
data sources like relational databases and NoSQL databases through
hands-on exercises. You will apply these best practices to complete a
design review of publicly available APIs with a small-scale software system
in order to design and implement RESTful APIs.
This book is intended for software developers who use data in projects.
It is also useful for data professionals who need to understand the methods
of data exchange and how to interact with business applications. Java
programming experience is required for the exercises.
xv
Introduction
Topics include
xvi
CHAPTER 1
Fundamentals
of RESTful APIs
Abstract
APIs are not new. They’ve served as interfaces that enable applications
to communicate with each other for decades. But the role of APIs has
changed dramatically in the last few years. Innovative companies
have discovered that APIs can be used as an interface to the business,
allowing them to monetize digital assets, extend their value proposition
with partner-delivered capabilities, and connect to customers across
channels and devices. When you create an API, you are allowing others
within or outside of your organization to make use of your service or
product to create new applications, attract customers, or expand their
business. Internal APIs enhance the productivity of development teams
by maximizing reusability and enforcing consistency in new applications.
Public APIs can add value to your business by allowing third-party
developers to enhance your services or bring their customers to you. As
developers find new applications for your services and data, a network
effect occurs, delivering significant bottom-line business impact. For
example, Expedia opened up their travel booking services to partners
through an API to launch the Expedia Affiliate Network, building a new
revenue stream that now contributes $2B in annual revenue. Salesforce
2
Chapter 1 Fundamentals of RESTful APIs
3
Visit https://ebookmass.com
now to explore a rich
collection of eBooks and enjoy
exciting offers!
Chapter 1 Fundamentals of RESTful APIs
4
Table 1-1. SOAP vs. REST comparison
Topic SOAP REST
Origin SOAP (Simple Object Access Protocol) was created in REST (Representational State Transfer) was
1998 by Dave Winer et al. in collaboration with Microsoft. created in 2000 by Roy Fielding at UC, Irvine.
Developed by a large software company, this protocol Developed in an academic environment, this
addresses the goal of addressing the needs of the protocol embraces the philosophy of the open
enterprise market Web
Basic Makes data available as services (verb + noun), for Makes data available as resources (nouns), for
Concept example, “getUser” or “PayInvoice” example, “user” or “invoice”
Pros Follows a formal enterprise approach Follows the philosophy of the open Web
Works on top of any communication protocol, even Relatively easy to implement and maintain
Chapter 1
5
Fundamentals of RESTful APIs
(continued)
Random documents with unrelated
content Scribd suggests to you:
With sands of sleep, from slumber’s sea.
I try my best awake to stay,
But I am tired out with play;
“I’ll never see him!” mother says,
And mother tells the truth—always!
BILLY BINKS
Once upon a time a little boy named Billy Binks set out to seek his
fortune. He traveled alone for many a weary mile, but at last he met
a little gray pony.
“Where are you going, Billy Binks?” neighed the pony.
“I am going to seek my fortune,” said Billy Binks.
“May I go, too?”
“If I take you, will you help me win my fortune?”
“Yes.”
“How?”
“I will carry you on my back and kick all your enemies with my hard
hoofs.”
“Very well, you may come along.”
Then they went on a little farther and met a cow.
“Where are you going, Billy Binks?” mooed the cow.
“I am going to seek my fortune,” answered Billy Binks.
“May I go, too?”
“If I take you, will you help me win my fortune?”
“Yes.”
“How?”
“I will moo, and toss your enemies on my sharp horns.”
“Very well, you may come.”
When they had walked on a little farther they met a dog.
“Where are you going, Billy Binks?” barked the dog.
“I am going to seek my fortune,” answered Billy Binks.
“May I go, too?”
“If I take you, will you help me win my fortune?”
“Yes.”
“How?”
“I will bark, and bite your enemies with my sharp teeth.”
“Very well, you may come.”
After walking a little farther they met a cat.
“Where are you going, Billy Binks?” mewed the cat.
“I am going to seek my fortune,” answered Billy Binks.
“May I go, too?”
“If I take you, will you help me win my fortune?”
“Yes.”
“How?”
“I will purr, and scratch your enemies with my sharp claws.”
“Very well, you may come.”
They continued their journey and presently met a raven.
“Where are you going, Billy Binks?” croaked the raven.
“I am going to seek my fortune.”
“May I go, too?”
“If I take you, will you help me win my fortune?”
“Yes.”
“How?”
“I will croak, and peck your enemies’ eyes out with my sharp
beak.”
“Very well, you may come.”
On and on they walked till at last they entered a deep, dark wood.
All day they journeyed through this forest, which grew denser and
darker as night came on.
“We are near a clearing in this wood,” croaked the raven, who had
been soaring above the treetops. “Let us keep right on.”
Suddenly all were startled by a bright light, the brightest any of
them had ever seen. It flashed out through the trees directly in front
of them. It fairly dazzled and blinded them. Then it as suddenly
disappeared, and left them standing terrified in the pitch-black
darkness of the night.
Again the light flashed out, and again disappeared.
“What can it be?” asked Billy Binks, hoarsely, as soon as he could
find his voice.
“Perhaps it is a lamp,” mewed the cat.
“No, it is too bright for a lamp,” answered Billy Binks.
“It might be a house on fire,” barked the dog.
“No, if it were, we could see the light all the time; and besides,
there is no house here. I have flown this way before,” answered the
raven.
“It may be a lighthouse,” said Billy Binks.
“No,” replied the raven, “the sea is miles from here. You all keep
still while I fly over the treetops and find out what it is.”
Billy Binks and his animal friends kept ever so quiet, while the
raven flew up and quickly disappeared in the darkness. It seemed
hours before he returned.
“Oh, my friends,” croaked the raven, alighting in their midst at last,
“you never saw such a sight! There’s the most horrible, monstrous
hob-goblin over there in the clearing. He has a nose as long as a
broomstick—”
“Oh! Oh! Oh!” cried Billy Binks and his friends.
“—Eyes as big as saucers and as green as the sea—”
“Oh! Oh! Oh!” cried Billy Binks and his friends.
“—And a mouth big enough to swallow us all!”
“Oh! Oh! Oh!” cried Billy Binks and his friends.
“He has a great fire blazing among some rocks. That is the light
you saw. When he walks in front of it you cannot see the light. That
is why you thought it disappeared.”
“I see! I see! I see!” said Billy Binks and his friends.
“He is busy melting gold, and he has piles of gold and jewels
hidden in his cave—”
“Ah, ha!” laughed Billy Binks, as he climbed bravely upon his gray
pony.
“His cave is full of nice plump field mice—”
“Mew! Mew!” cried the cat, as she scrambled up behind Billy
Binks.
“In the bushes back of the cave live many rabbits—”
“Bow-wow!” barked the dog, as he bounded toward Billy Binks.
“Near the cave is a large green meadow, with the sweetest grass
and the coolest brook in the world—”
“Moo! Moo!” lowed the cow, as she, too, hurried up beside Billy
Binks.
“And there is a tall tree that will make a fine home for me,” finished
the raven, as she flew over Billy Binks’s head.
“Come on, friends,” whispered Billy Binks, boldly. “It is time to win
my fortune. Remember you have all promised to help me.”
“Yes, yes, I’ll help. And I think I see my fortune, too,” answered
each of the animals, now as bold as Billy Binks.
Softly, quietly, and slowly they crept through the forest. Presently
they came to the clearing. There stood the ugly, black hob-goblin,
bending over his fire. His back was turned toward them.
“Now!” shouted Billy Binks, and they all rushed at the terrible
monster.
The raven dashed into his face and pecked at his large green
eyes.
The cat scratched great gashes in his long nose.
The dog bit him, and the horse kicked him.
The enraged cow rushed upon him with lowered head, caught him
on her horns, and tossed him as high as the treetops.
Then the cow began to bellow.
The dog began to howl.
The cat began to waul.
The raven began to caw.
The pony began to prance.
And Billy Binks began to shout with all his might.
Such a frightful din that old hob-goblin had never heard! He picked
himself up from the sharp rocks where he had fallen, and dashed
away with might and main through the forest. If he hasn’t stopped,
he is running still.
“Ho, ho!” cried Billy Binks, springing from the gray pony and
running to the mouth of the cave. “This heap of gold and this pile of
jewels will do for my fortune. If you carry them safely home for me,
Pony, I will build you a beautiful stable, and you shall have a full crib
of oats before you all the rest of your life. That will be your fortune.”
“This cave, full of good, plump mice, is my fortune,” called the cat,
as she pounced on the first unlucky mouse.
“All these rabbits shall be my fortune,” barked the dog, as he set
off in hot haste after a fleeing bunny.
“And this green meadow is my fortune,” mooed the cow, as she
began to crop the sweet grass.
“Who could have a better fortune than this?” croaked the raven,
flying to the top of a tall tree.
So Billy Binks said “Good-by” to his friends, and left them each
with his fortune. He quickly bagged the gold and jewels, threw them
across the pony’s back, and mounting, hurried off homeward.
The pony smelled oats all the way, while Billy Binks saw castles
and lands on all sides.
Some Things to think About
WHEN THE LITTLE BOY RAN AWAY
in stant ly
Once upon a time there was a poor old woman living in a village of
a far country. She had gathered some beans and was making ready
to cook them. She built a fire of sticks, but, as these were damp, they
did not burn well. So she thrust in a handful of dry straw. Now the
flames leaped up, and the sticks snapped and crackled in the blaze.
A live red coal flew out of the fire, fell on the ground beside a
straw, and lay there smoking. Just then a bean dropped from the pot
which the old woman was filling, rolled away, and came to rest close
to the coal and the straw.
“Hullo, Mr. Coal,” said the straw. “How you smoke! Are you
frightened? Where did you come from?”
“I just sprang out of that fire,” answered the coal. “Had I not
jumped just as I did, I should now be nothing but ashes. My, look at
that blaze!”
“I, too, jumped in the nick of time,” spoke up the bean. “That cruel
old woman was just pouring me into the pot when I leaped over the
edge, and here I am.”
“Yes, here you are, silly thing,” broke out the coal and the straw
together. “But what are you going to do? As soon as the old woman
turns around she will spy you, then back you’ll go into the pot. It’s
hotter now than when you left it.”
“Don’t bother about me; think of yourselves,” answered the bean,
angrily. “When the old woman picks me up, she’ll tread on you, Mr.
Coal, and crush your life out. And you, Mrs. Straw, she’ll stick into
the blaze. It’s hotter there than in the pot.”
“Come, come,” said the straw, softly, “let’s not quarrel. Let’s be
friends and stick together. Perhaps we can save ourselves yet.”
“You are quite right, Mrs. Straw,” said the coal.
The bean said nothing, but she listened eagerly to the plans of the
two others. These soon agreed to travel together to a far country,
where they hoped to find their fortune. They set out without delay,
and the bean rolled along behind.
Soon the three travelers came to a little gurgling brook. It seemed
to them a mighty rushing and roaring torrent.
“Oh, dear, what shall we do now?” asked the bean, speaking for
the first time since the journey began. “We can never get across
these awful waters. Hear them thunder down the rocky cliffs!”
“Don’t worry, little Bean,” said the straw, proudly. “I’ll help you and
Mr. Coal across in a twinkling.”
Thereupon the straw laid herself across the stream. She was just
long enough to reach from bank to bank.
“Now walk over the bridge, Mr. Coal and Miss Bean,” called the
straw.
The coal hastened on to the straw bridge while the bean watched
in wonder. All went well until the middle of the stream was reached,
when the bridge bent so low under the weight of the coal and the
waters thundered so loudly that the coal stopped in fright.
The coal stood still for only a moment. But, alas, that was a
moment too long.
The dry straw smoked, burst into a tiny flame, and broke in two.
Down fell the coal into the water below and was instantly drowned.
The burning straw bridge also fell into the water, which put out the
flames, and the two pieces of straw went floating away down stream.
All this the little bean saw, watching safely from the bank. And she
thought it the funniest thing that ever happened. So she laughed and
she laughed—until she burst!
This would have been the end of little Miss Bean, had not a tailor
passed that way just then. He was sorry for the poor bean, so he
picked up the two parts tenderly, and quickly sewed them together.
But the thread that he used was black. And ever since that time
some beans have a black seam around them.
FRIENDS
—L. G. Warner.
HELP ONE ANOTHER