Book Image

Java EE 8 Application Development

Book Image

Java EE 8 Application Development

Overview of this book

Java EE is an Enterprise Java standard. Applications written to comply with the Java EE specification do not tie developers to a specific vendor; instead they can be deployed to any Java EE compliant application server. With this book, you’ll get all the tools and techniques you need to build robust and scalable applications in Java EE 8. This book covers all the major Java EE 8 APIs including JSF 2.3, Enterprise JavaBeans (EJB) 3.2, Contexts and Dependency Injection (CDI) 2.0, the Java API for WebSockets, JAX-RS 2.1, Servlet 4.0, and more. The book begins by introducing you to Java EE 8 application development and goes on to cover all the major Java EE 8 APIs. It goes beyond the basics to develop Java EE applications that can be deployed to any Java EE 8 compliant application server. It also introduces advanced topics such as JSON-P and JSON-B, the Java APIs for JSON processing, and the Java API for JSON binding. These topics dive deep, explaining how the two APIs (the Model API and the Streaming API) are used to process JSON data. Moving on, we cover additional Java EE APIs, such as the Java API for Websocket and the Java Message Service (JMS), which allows loosely coupled, asynchronous communication. Further on, you’ll discover ways to secure Java EE applications by taking advantage of the new Java EE Security API. Finally, you’ll learn more about the RESTful web service development using the latest JAX-RS 2.1 specification. You’ll also get to know techniques to develop cloud-ready microservices in Java EE.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

The JSON-P Streaming API


The JSON-P Streaming API allows sequential reading of a JSON object from a stream (a subclass of java.io.OutputStream or a subclass of java.io.Writer). It is faster and more memory efficient than the Model API, however, the trade-off is that it is more limited, since the JSON data needs to be read sequentially and we cannot access specific JSON properties directly the way the Model API allows.

Generating JSON data with the Streaming API

The JSON Streaming API has a JsonGenerator class that we can use to generate JSON data and write it to a stream. This class has several overloaded write() methods, which can be used to add properties and their corresponding values to the generated JSON data.

The following code sample illustrates how to generate JSON data using the Streaming API:

    package net.ensode.javaee8book.jsonpstreaming; 
 
    //other imports omitted 
    import javax.json.Json; 
    import javax.json.stream.JsonGenerator; 
    import javax.json.stream.JsonParser...