Notation (JSON) data by using the object model and the streaming
also demonstrates how to consume the generated JSON
Time to Complete
Approximately 45 minutes
JSON is a lightweight data-interchange format that is used in
web applications and REST WebServices. With the popularity of
JSON and the availability of many JSON libraries for the Java
platform (for example,
google-gson), there is a need to standardize
how developers create and consume JSON.
Java Platform, Enterprise Edition 7 (Java EE 7) introduces the JSON Processing API, which provides a standard API to parse and generate JSON so that the applications can rely upon a portable API. The JSON Processing API contains the following two packages:
javax.jsonpackage contains a reader interface, a writer interface, and a model builder interface for the object model (similar to the DOM API for XML).
javax.json.streampackage contains a parser interface and a generator interface for the streaming model (similar to the StAX API for XML).
In this tutorial, you learn how to:
- Create a Java EE 7 web application
- Develop two Java servlets to generate JSON data:
index.htmlto invoke the two servlets
- Deploy the project to the GlassFish server and verify the
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Download and install the latest JDK from this link (Java SE 7u21 recommended).
- Download and install NetBeans 7.3.1 with Java EE, which includes GlassFish 4 (Java EE download bundle) from this link. During installation, be sure to select the check box to install GlassFish. JUnit is an optional installation and is not required for this tutorial.
Before starting this tutorial, you should:
- Have installed the required software.
- Ensure that NetBeans is running.
- Download and unzip the
Creating a Web Application
In this section, you create a Java EE 7 web application in the NetBeans IDE.
Developing the Servlets
In this section, you create two servlets:
generates JSON data with the object model, and
generates JSON data with the streaming model.
In this section, you modify
to generate JSON data with the object model. You will build a
JSON representation of an
Item with the object
In this section, you modify the
servlet to generate JSON data with the streaming model. You will
build a JSON representation of an
Item with the
streaming model API.
In this section, you invoke the two servlets by changing
Testing the JSON Generation in the Servlets
this section, you verify the JSON generation in the two
servlets by deploying and running the project .
retrieves data via an AJAX call, and displays it on the HTML
this section, you verify the response from the JSON client.
You do that by deploying and running the project .
In this tutorial, you learned how to:
- Create a Java EE 7 web application
- Generate JSON data by using the Java API for JSON processing
- Java EE 7 Tutorial
353:Java API for JSON Processing
- Java API for JSON Processing presentation from JavaOne on YouTube.
- To learn more about Java EE, refer to additional OBEs in the Oracle Learning Library.
- Lead Curriculum Developer: Anjana Shenoy
- Other Contributors: Eduardo Moranchel, Matt Heimer
- Editor: Susan Moxley
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.