The purpose of this tutorial is to create a sticker story web application in which users can collaborate to create a story by dragging images into a book or canvas. When a sticker is dropped onto the canvas, it is rendered in all other web browsers that are connected to the server. A sticker's state is also saved on the server so information is saved if a session ends.
Time to Complete
Approximately 1 hour
You will start with a bare client interface and a minimal REST server. To complete the sticker story application, you'll use the Java API for RESTful Web Services (JAX-RS) 2.0 and the Java API for JSON Processing to handle client-server communication. Finally, you'll save the data in the server to for saving a sticker's current state.
The following is a list of software requirements needed to accomplish this tutorial:
- Download and install the Java Platform, Enterprise Edition 7 (Java EE 7) software development kit from http://www.oracle.com/technetwork/java/javaee/downloads/index.html
- Download and install NetBeans integrated development environment (IDE) 7.3.1 from http://www.netbeans.org/downloads/index.html
- Download and install Oracle GlassFish Server 4.0 from http://glassfish.java.net/download.html
Before starting this tutorial, you should have:
- Knowledge of the Java programming language.
- Intermediate knowledge of Java EE 7.
- Basic knowledge of HTML.
Introduction to JAX-RS 2.0 and the Java API for JSON Processing
This section gives a brief introduction to the Java API for RESTful Web Services and the Java API for JSON Processing.
JAX-RS (JSR-339) makes it easy for developers to build RESTful web services in Java EE 7. With JAX-RS, you can build simple, lightweight and fast applications by using a stateless comunication protocol (typically HTTP) and standarized interfaces.
Some principles that RESTful web services typically include are:
- Resource identification through URIs, which provide a global addressing space for resource and service discovery.
- Uniform interface, where resources are manipulated using a fixed set of four operations: PUT, GET, POST, and DELETE.
- Self-descriptive messages, that allow resources to be decoupled from their representation.
- Stateful interaction through links, which allow self-contained requests for client-server interactions.
JAX-RS 2.0 simplifies development for REST architectural-style applications and services by providing Java annotations. These annotations might contain resources configuration, helper classes, artifacts generated, and the resources exposed to clients once the application is deployed.
JSON is a lightweight data exchange format widely used in web services and other connected applications. The Java API for JSON Processing (JSR-353) provides an API to parse, transform, and query JSON data using the object model or the streaming model.
Preparing the Project
In this section, you create a Java EE 7 web application on which you’ll build the book registration application on.
The sticker-story and sticker-story-rest-server projects are running. You are now ready to start modifying them.
Generating a JSON List of Images
In this section, you learn how to generate a JSON list that contains the names of the sticker images that are available for the users to interact with. To accomplish this, you must modify the getImageList() method of the StickersResource class. Once the list of sticker names is generated, they can be accessed in the following URL: http://localhost:8080/sticker-story-rest-server/rest/stickers.
Retrieving the JSON List of Images
In this section, you retrieve the JSON list of sticker images generated in Section 3. To accomplish this, you modify the getAllStickers() method of the StickerSheet class.
A list of images is now obtained by the client.
Displaying the Images on the Client
In this section you learn how to display the sticker images in the web browser. To accomplish this, you will modify the StickerServlet class using JAX-RS 2.0 clients to make asynchronous calls to the server.
Creating an Asynchronous Request to the Server
Defining the Callback Function
Saving Stickers' States in the REST Server
In this section, you learn how to store a sticker’s state in the server. To accomplish this, you must:
- Create a client-side request with the sticker’s data
- Send the sticker’s data to the server
- Store the new sticker’s data in the server
Preparing the Request to the Server
In this section, you prepare an HTTP POST request to the server. These requests are made when one of the following events occur:
- A new client (or user) connects to the server.
- A sticker is dropped in the sticker board.
Requesting Current Stickers
When a new connection is made to the server, the onOpen() method is invoked. This method requests the list of active stickers when connecting to the server and keeps a list of active sessions in the server.
Handling Requests in the Server
In this section you process the HTTP POST and GET requests you defined in the previous sections.
In this tutorial you learned how to use the Java API for RESTful Web Services (JAX-RS 2.0) and the Java API for JSON Processing to build an interactive web application that saves an object's properties in the server.
You also learned how to:
- Use the Client class to build and execute requests for calling a REST service.
- Use the WebTarget class to specify a Client's target for requests.
- Build, process and manipulate JSON files.
- Model and create a save requests.
- Send the stickers' data to the server using HTTP POST.
- Request the stickers' data from the server using HTTP GET.
- Receive JSON data and store it as objects in the server.
For more information about the topics in this tutorial, see:
- JSR 353: Java API for JSON Processing
- JSR 339: JAX-RS 2.0: The Java API for RESTful Web Services
- Java EE 7 and JAX-RS 2.0
- JAX-RS Client API and GlassFish 4 - Logging HTTP messages
- Java EE 7: New Features.
- The Java EE 7 Tutorial
- The Java EE 7 Tutorial chapter on JSON Processing
- The Java EE 7 Tutorial chapter on Building RESTful Web Services With JAX-RS
- To learn more about Java EE 7, refer to additional OBEs in the Oracle Learning Library.
- Lead Curriculum Developer: Miguel Salazar
- Other Contributors: Eduardo Moranchel
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.