32,36 €
An enterprise Java developer's guide to learning JAX-RS, context and dependency injection, JavaServer Faces (JSF), and microservices with Eclipse MicroProfile using the latest features of Jakarta EE
Key Features
Book Description
Jakarta EE is widely used around the world for developing enterprise applications for a variety of domains. With this book, Java professionals will be able to enhance their skills to deliver powerful enterprise solutions using practical recipes.
This second edition of the Jakarta EE Cookbook takes you through the improvements introduced in its latest version and helps you get hands-on with its significant APIs and features used for server-side development. You'll use Jakarta EE for creating RESTful web services and web applications with the JAX-RS, JSON-P, and JSON-B APIs and learn how you can improve the security of your enterprise solutions. Not only will you learn how to use the most important servers on the market, but you'll also learn to make the best of what they have to offer for your project. From an architectural point of view, this Jakarta book covers microservices, cloud computing, and containers. It allows you to explore all the tools for building reactive applications using Jakarta EE and core Java features such as lambdas. Finally, you'll discover how professionals can improve their projects by engaging with and contributing to the community.
By the end of this book, you'll have become proficient in developing and deploying enterprise applications using Jakarta EE.
What you will learn
Who this book is for
This book is for Java EE developers who want to build enterprise applications or update their legacy apps with Jakarta EE's latest features and specifications. Some experience of working with Java EE and knowledge of web and cloud computing will assist with understanding the concepts covered in this book.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 277
Veröffentlichungsjahr: 2020
Copyright © 2020 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Kunal ChaudhariAcquisition Editor: Denim PintoContent Development Editor: Tiksha LadSenior Editor: Afshaan KhanTechnical Editor: Sonam PandeyCopy Editor: Safis EditingProject Coordinator: Francy PuthiryProofreader: Safis EditingIndexer: Pratik ShirodkarProduction Designer: Shankar Kalbhor
First published: June 2019 Second edition: May 2020
Production reference: 1280520
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-83864-288-4
www.packt.com
Packt.com
Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Fully searchable for easy access to vital information
Copy and paste, print, and bookmark content
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
(Foreword from the previous edition)
It is a measure of the penetration, longevity, and quality of Java EE technology that, in 2018, my friend Elder Moraes asked me to write the foreword for his book about Java EE 8. My personal involvement with Java EE goes back to the days preceding J2EE 1.4 in 2001. Since then, I have had the great honor of leading or co-leading the community teams that have developed JavaServer Faces and, later, Servlet, two of thetechnologies Elder covers in this book. During that time, I tried to follow the model of servant-leader, and I think the result has been a very engaged community that has a real stake in the continued success of Java EE.
When writing this foreword, I want to focus on four Cs: curation, cohesion, current, and completeness. So much has been written about Java EE over the years, and continues to be written, that the task of writing a book, particularly one in the useful cookbook format, involves a lot of curation. From the range of all possible things that people are doing with Java EE, which is vast, Elder has presented a curation of what he thinks are the most useful and essential things. Elder is well positioned to decide what goes in and what stays out. Elder has been consulting and working with Java EE for nearly as long as I have, but from the more practical perspective of the user.
Technical books that follow the cookbook pattern frequently suffer from a feeling of disjointedness. Not this book. Elder has put a great deal of effort into ensuring cohesion. Over the years, the technologies of Java EE have sometimes been criticized for not being cohesive enough with each other. This is something Sun made a conscious effort to address, starting with Java EE 6, and which Oracle continued working on in Java EE 8. Elder has leveraged this effort to seek out and present the best way to leverage the synergy of all the technologies of Java EE 8 to maximum effect.
The world outside Java EE has continued to evolve, and this has changed the way people use Java EE dramatically. The challenge for any architect on a multiyear software effort, with a service lifetime of at least a decade, is how to keep it maintainable even while the surrounding technology landscape changes. Elder has accounted for this with two excellent chapters about microservices and Docker. These two technologies provide a great complement to the power of Java EE, but also have numerous pitfalls. Elder helps you avoid the pitfalls while getting the most out of these current trends.
Finally, completeness. Many technology cookbooks stop short of providing complete reference sort of material, but Elder goes much deeper. It's almost to the point that the term cookbook does not do this book justice. Perhaps a more correct label would be complete restaurant management with supply chain logistics and a cookbook on top. Elder covers the current popular app servers on which people are running Java EE, continuous integration and pipelines, reactive programming, and more. Coming back to the curation point, it's all there, and in depth.
I hope you have success with Java EE and with its successor, Jakarta EE from the Eclipse Foundation.
Ed Burns
Consulting Member of Technical Staff at Oracle
Specification Lead of JSF and Servlet
Elder Moraes helps Jakarta EE developers build and deliver secure, fast, and available applications so that they are able to work on great projects. He is passionate about content sharing; he does it by speaking at international events, blogging, and writing articles. He has been working with Java since 2002 and has developed applications for different industries. As a board member at SouJava, he led the Java EE 8 - The Next Frontier initiative, interviewing some world-class Java EE experts.
Deepak Vohra is a consultant and a principal member of the NuBean.com software company. He is a Sun Certified Java Programmer and Web Component Developer and has worked in the fields of XML, Java programming and Java EE for ten years. Deepak is the co-author of the Apress book, Pro XML Development with Java Technology and is also the author of the Packt Publishing books JDBC 4.0 and Oracle JDeveloper for J2EE Development, Processing XML Documents with Oracle JDeveloper 11g, EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g, and Java EE Development in Eclipse IDE, and Advanced Java EE Development with WildFly.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Jakarta EE Cookbook Second Edition
Dedication
About Packt
Why subscribe?
Foreword
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Jakarta EE 9 is on the way – what now?
Download the example code files
Download the color images
Conventions used
Sections
Getting ready
How to do it...
How it works...
There's more…
See also
Get in touch
Reviews
New Features and Improvements
Running your first Jakarta Bean Validation 2.0 code
Getting ready
How to do it...
How it works...
See also
Running your first Jakarta CDI 2.0 code
Getting ready
How to do it...
How it works...
There's more...
See also
Running your first JAX-RS 2.1 code
Getting ready
How to do it...
How it works...
See also
Running your first JSF 2.3 code 
Getting ready
How to do it...
How it works...
There's more...
See also
Running your first JSON-P 1.1 code 
Getting ready
How to do it...
How it works...
See also
Running your first JSON-B 1.0 code 
Getting ready
How to do it...
How it works...
See also
Running your first Jakarta Servlet 4.0 code 
Getting ready
How to do it...
How it works...
There's more...
See also
Running your first Jakarta Security code 
Getting ready
How to do it...
How it works...
There's more...
See also
Running your first MVC 1.0 code 
Getting ready
How to do it...
How it works...
See also
Server-Side Development
Using Jakarta CDI to inject context and dependencies
Getting ready
How to do it...
How it works...
There's more...
See also
Using Jakarta Bean Validation for data validation
Getting ready
How to do it...
How it works...
See also
Using Jakarta Servlet for request and response management
Getting ready
How to do it...
How it works...
There's more...
See also
Using Server Push to make objects available beforehand
Getting ready
How to do it...
How it works...
There's more...
See also
Using EJB and JTA for transaction management
Getting ready
How to do it...
How it works...
There's more...
See also
Using EJB to deal with concurrency
Getting ready
How to do it...
How it works...
There's more...
See also
Using JPA for smart data persistence
Getting ready
How to do it...
How it works...
See also
Using EJB and JPA for data caching
Getting ready
How to do it...
How it works...
There's more...
See also
Using Jakarta Batch processing
Getting ready
How to do it...
How it works...
See also
Building Powerful Services with JSON and RESTful Features
Building server-side events with JAX-RS
Getting ready
How to do it...
How it works...
There's more...
See also
Improving service's capabilities with JAX-RS and Jakarta CDI
Getting ready
How to do it...
How it works...
There's more...
See also
Easing data and objects representation with Jakarta JSON Binding
Getting ready
How to do it...
How it works...
See also
Parsing, generating, transforming, and querying JSON objects using Jakarta JSON Processing
Getting ready
How to do it...
How it works...
See also
Web and Client-Server Communication
Using servlets for request and response management
Getting ready
How to do it...
How it works...
The load-on-startup servlet
A servlet with initParams
The asynchronous servlet
See also
Building a UI with template features using JSF
Getting ready
How to do it...
How it works...
See also
Improving response performance with Server Push
Getting ready
How to do it...
How it works...
There's more...
See also
Security of the Enterprise Architecture
Domain protection with authentication
Getting ready
How to do it...
How it works...
See also
Granting rights through authorization
Getting ready
How to do it...
How it works...
See also
Protecting data confidentiality and integrity with SSL/TLS
Getting ready
How to do it...
How it works...
There's more...
See also
Using declarative security
Getting ready
How to do it...
How it works...
See also
Using programmatic security
Getting ready
How to do it...
How it works...
See also
Reducing Coding Effort by Relying on Standards
Preparing your application to use a connection pool
Getting ready
How to do it...
There's more...
See also
Using messaging services for asynchronous communication
Getting ready
How to do it...
How it works...
See also
Understanding a servlet's life cycle
Getting ready
How to do it...
How it works...
See also
Transaction management
Getting ready
How to do it...
How it works...
See also
Deploying and Managing Applications on Major Jakarta EE Servers
Understanding Apache TomEE
Getting ready
How to do it...
There's more...
See also
Eclipse GlassFish
Getting ready
How to do it...
There's more...
See also
Red Hat WildFly
Getting ready
How to do it...
There's more...
See also
Building Lightweight Solutions Using Microservices
Building microservices from a monolith
Getting ready
How to do it...
Building a monolith
Building microservices from the monolith
How it works...
There's more...
See also
Building decoupled services
Getting ready
How to do it...
How it works...
See also
Building an automated pipeline for microservices
Getting ready
How to do it...
Continuous integration
Continuous delivery
Continuous deployment
There's more...
See also
Determining the state of a microservice by using the MicroProfile Health Check API
Getting ready
How to do it...
How it works...
There's more...
See also
Generating and/or monitoring metrics with the MicroProfile Metrics API
Getting ready
How to do it...
How it works...
There's more...
See also
Exposing API documentation using the MicroProfile OpenAPI
Getting ready
How to do it...
How it works...
There's more...
See also
Using Multithreading on Enterprise Context
Building asynchronous tasks with returning results
Getting ready
How to do it...
How it works...
See also
Using transactions with asynchronous tasks
Getting ready
How to do it...
How it works...
See also
Checking the status of asynchronous tasks
Getting ready
How to do it...
How it works...
See also
Building managed threads with returning results
Getting ready
How to do it...
How it works...
See also
Scheduling asynchronous tasks with returning results
Getting ready
How to do it...
How it works...
See also
Using injected proxies for asynchronous tasks
Getting ready
How to do it...
How it works...
See also
Using Event-Driven Programming to Build Reactive Applications
Building reactive applications using asynchronous servlets
Getting ready
How to do it...
How it works...
See also
Building reactive applications using events and observers
Getting ready
How to do it...
How it works...
See also
Building reactive applications using WebSocket
Getting ready
How to do it...
How it works...
See also
Building reactive applications using message-driven beans
Getting ready
How to do it...
How it works...
See also
Building reactive applications using Jakarta RESTful Web Services
Getting ready
How to do it...
How it works...
See also
Building reactive applications using asynchronous session beans
Getting ready
How to do it...
How it works...
See also
Using lambdas and CompletableFuture to improve reactive applications
Getting ready
How to do it...
How it works...
See also
Rising to the Cloud - Jakarta EE, Containers, and Cloud Computing
Building Jakarta EE containers using Docker
Getting ready
How to do it...
How it works...
See also
Using Oracle Cloud Infrastructure for container orchestration in the cloud
Getting ready
How to do it...
Using Jelastic for container orchestration in the cloud
Getting ready
How to do it...
Using OpenShift for container orchestration in the cloud
Getting ready
How to do it...
Using AWS for container orchestration in the cloud
Getting ready
How to do it...
Appendix - The Power of Sharing Knowledge
Introduction
Why contributing to the Adopt a JSR program can make you a better professional
Understanding the Adopt a JSR program
Collaborating on the future of Jakarta EE
Setting yourself up for collaboration
Setting aside a specific time for it
Choosing where you'll concentrate your efforts
Do it!
The secret to unsticking your career, your project, and even your life!
Other Books You May Enjoy
Leave a review - let other readers know what you think
Jakarta EE is a mature platform that's widely used around the world. It is also a standard that has evolved through the hard work of individuals, vendors, groups leaders, and communities. It has a whole market and ecosystem around it, with millions of users, which also means a big and active community that is always willing to help it move forward.
For those reasons, the purpose of this book is to meet the needs of those professionals who depend on Jakarta EE to deliver really awesome enterprise solutions, not only talking about real solutions to real problems but also showing how to implement those solutions in a practical way.
The book starts with a quick overview of what Jakarta EE is and the improvements in version 8. Then, it takes you on a hands-on journey through the most important APIs.
You will learn how to use Jakarta EE for server-side development, web services, and web applications. You will also take a look at how you can properly improve the security of your enterprise solutions.
No Jakarta EE application is good enough if it doesn't follow the standards, and for that, you can count on the Jakarta EE application servers. This book will teach you how to use the most important servers on the market and get the best that they have to offer for your project.
From an architectural point of view, the book will cover microservices, cloud computing, and containers. Also, it will not forget to give you all the tools you need to build a reactive Jakarta EE application using not only Jakarta EE features, but also Java core features such as lambdas and completable futures.
The whole Java world is all about the community, so we will also show you how community-driven professionals can improve the results of their projects and even go to higher levels in their careers.
This book was based on a concept that I call The Five Mistakes That Keep Jakarta EE Professionals Away From Great Projects. I am ruining my career when I don't do the following things:
Keep myself up to date
Know the APIs (having an overview of all of them and a mastery of the most important ones)
Know the most commonly used Jakarta EE application servers
Know advanced architectures
Share what I know
So, the book is a straightforward, practical, and helpful solution to each one of these mistakes. I can say with confidence that dealing with them properly can change the careers and lives of many developers around the world. I know because they've changed mine, for good.
This book is made for developers who would like to learn how to meet real enterprise application needs using Jakarta EE 8. They should be familiar with application development and need to have knowledge of at least basic Java, some minimal knowledge of Jakarta EE, the basic concepts of cloud computing, and web services.
You should want to learn how to combine a bunch of APIs in a secure and fast solution, and for this, you need to know how the APIs work and when to use each one.
If when you got this book in your hands we already have Jakarta EE 9 released, please read the To get the most out of this book section ahead, for directions on how to make 100% of the code of this book work without any issues.
Chapter 1, New Features and Improvements, explains the main changes to the Jakarta EE 8 specification and what you can do with them. It also shows the new features and briefly explores the benefits of them. All these topics are supported by code examples.
Chapter 2, Server-Side Development, deep-dives into the most important APIs and the most commonly used features for server-side development. You will go through real recipes for solving real problems.
Chapter 3, Building Powerful Services with JSON and RESTful Features, creates web services for different enterprise scenarios. You will go deep into the JAX-RS, JSON-P, and JSON-B APIs.
Chapter 4, Web- and Client-Server Communication, deals with the communication generated by web applications in a fast and reliable way using the latest Jakarta EE 8 features, such as HTTP2 and Server Push.
Chapter 5, Security of Enterprise Architecture, gives you information on various tools using the best Jakarta EE features to create secure architectures.
Chapter 6, Reducing the Coding Effort by Relying on Standards, describes the services and features that Jakarta EE application servers give to the applications they host. Those features not only enable you to rely on a standard and build your application based on it, but also allow you to write less code, as you don't need to implement features that have already been implemented by the server.
Chapter 7, Deploying and Managing Applications on Major Jakarta EE Servers, describes the use of each of the most commonly used Jakarta EE application servers on the market, giving special attention to the way you deploy and manage them.
Chapter 8, Building Lightweight Solutions Using Microservices, helps you to understand how microservice architectures work and how you can easily use Jakarta EE 8 to build microservices and/or break down your monoliths in order to implement this paradigm.Continuous delivery and continuous deployment are also described, as no successful microservice project is complete without a mature building and deployment process.
Chapter 9, Using Multithreading on Enterprise Context, describes the use of multithreading and concurrency when building enterprise applications.
Chapter 10, Using Event-Driven Programming to Build Reactive Applications, describes the use of Jakarta EE 8 and core Java to create low-latency, efficient, and high-throughput applications.
Chapter 11, Rising to the Cloud – Jakarta EE, Containers, and Cloud Computing, describes how to combine Jakarta EE and containers to run applications on the cloud.
Appendix, The Power of Sharing Knowledge, describes how the community is vital for the whole Jakarta EE ecosystem (even if you don't know about it), and looks at how you can improve your own daily work by joining the Adopt a JSR initiative.
The chapter also describes how sharing knowledge is a powerful tool for improving your career and explains what it has to do with Jakarta EE (it has everything to do with Jakarta EE!).
You should be familiar with application development and you need to have at least basic knowledge of Java and Jakarta EE. Basic knowledge of cloud computing and web services is also assumed.
Software/hardware covered in the book
OS requirements
Open JDK 8 or superior
Linux/macOS/Windows
Maven 3.5
Linux/macOS/Windows
Eclipse GlassFish 5.1
Linux/macOS/Windows
Docker CE 18
Linux/macOS/Windows
Git SCM 2.16
Linux/macOS/Windows
If when trying to run any of the code in this book you got a message saying that the "address is already in use" or something like it, you probably have an other service/application using the same port that the example is trying to use. Closing it will fix this issue.
By the time this second edition is released, we will just be a few months away from the release of Jakarta EE 9! Because of this, you may be wondering, "why not publish the book when 9 is alive and kicking?".
Good question! And the answer is equally clear: because no APIs/features will be changing in this release. So, what's changed that would justify a new release?
Because of one thing in the Jakarta EE field known as big bang. If you've been following Jakarta EE for a while now, you should be aware of it. If not, let me quickly explain it to you.
Due to Java EE being transferred from Oracle to Eclipse Foundation, it was rebranded to Jakarta EE, and due to this, we had the Jakarta EE 8 release. That's why we have the second edition of this book!
As part of the branding and intellectual property process, another thing needed to be changed: the javax namespaces.
The following examples have been taken from this very book, and all of them use the javax namespace:
So, all the APIs under Jakarta EE have to change their namespaces from javax to jakarta. This same list in Jakarta EE 9 will look like this:
Since this book was written to help you deliver the most incredible applications using Jakarta EE 8, I need to answer the following, and most important, question: how does this change affect you and/or your code if you migrate from 8 to 9?
The simple and quick answer is that your code/project will break. Period.
This isn't as bad as it sounds. At the end of the day, Jakarta EE has a huge community of developers around the world (probably one of the biggest communities), and most of them are developers like you that will face these same problems if there's no way out.
So, yes, you'll need to change the imports for all the classes in your project that use the javax namespace. And no, you won't need to do this manually (unless you want to).
While I'm writing this, there is already one tool that is being developed by the Apache Tomcat folks. You can try it out and follow the project for more information at https://github.com/apache/tomcat-jakartaee-migration.
I'm pretty sure that even more tools will become available soon. Stay tuned!
For more information about the big bang, you can read this amazing blog post by Mike Milinkovich: https://eclipse-foundation.blog/2020/01/16/moving-forward-with-jakarta-ee-9/.
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packtpub.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/eldermoraes/javaee8-cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838642884_ColorImages.pdf .
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Then, two key methods from SseResource do their work."
A block of code is set as follows:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0</version> <scope>provided</scope></dependency>
Any command-line input or output is written as follows:
Info: destroy
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "We defined the security rules and roles through our code (the program). There's another approach called declarative security."
In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).
To give clear instructions on how to complete a recipe, use these sections as follows.
This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.
This section contains the steps required to follow the recipe.
This section usually consists of a detailed explanation of what happened in the previous section.
This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.
This section provides helpful links to other useful information for the recipe.
Feedback from our readers is always welcome.
General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
