GWT RPC and Serialization
One of the most important pieces of the GWT framework is the GWT Remote Procedure Call (RPC) mechanism. This RPC mechanism makes it easy for a GWT application client to make a call to server-side services. GWT RPC makes it simple to get data between the client and the server.
What is Serialization?
Any object that needs to be sent between the client and server has to be a GWT serializable type. A type is serializable and can be used in a service interface if the type:
- is primitive, such as char, byte, short, int, long, boolean, float, or double;
- is String, Date, or a primitive wrapper such as Character, Byte, Short, Integer, Long, Boolean, Float, or Double;
- is an array of serializable types (including other serializable arrays);
- is a serializable user-defined class; or
- has at least one serializable subclass
GWT Serialization demo – The Contact List Application
A simple GWT application that gets a list of Contacts from the server will be used to demonstrate GWT RPC’s serialization mechanism. The Contact List Application is a simple GWT application where a user can click on a button to make a GWT service call to retrieve a list of contacts.
Creating a user defined GWT Serializable Class
A user defined class is serializable if:
- the class is assignable to IsSerializable or java.io.Serializable, either because it implements one of these interfaces, or because it is derived from a superclass that implements one of these interfaces.
- all the class’s non-final, non-transient instance fields are serializable
- the class has a public default (zero argument) constructor
The heart of the Contact List Application is the Contact class. The Contact class consists of a HashMap of Strings that stores contact information. The HashMap key is a String that signifies the type of contact information, e.g. name, e-mail address, street address, phone number, etc. The Contact List Application’s client UI will consist of a display area and a button that allows the user to make a GWT service call to retrieve all the Contacts. The GWT service will return an ArrayList of Contact objects.
Type Arguments – How to make serializable Collections in GWT
Collection classes such as java.util.Map and java.util.List represent a collection of Object instances. To make collections serializable, you must specify the type of objects the collections are expected to contain. This is achieved through the use of a special Javadoc annotation: @gwt.typeArgs. This annotation is necessary to enable the GWT proxy generator to create efficient code. Defining the item type for a collection ensures that the collection only ever contains objects of that item type, or a subclass thereof. Be careful to only add objects of the asserted item type to a collection. Adding an object to a collection that violates its asserted item type will lead to undefined and anomalous behavior.
The code below shows you how to define and implement the Serializable Contact class. This demonstrates how to create a serializable user-defined class that has a Collection as a field. As you can see from the echoContactList method, there is no need to specify the name of the field in the @gwt.typeArgs declaration since it can be inferred.
The next step is to define and implement a service that returns a list of Contacts. The code below shows you how to define the service as well as how to annotate its method parameters and return types. As you can see, the parameter annotations must include the name of the parameter they are annotating along with the collection item type. Return type annotations do not need to include the name of what is being returned.
A Note about GWT RPC and Polymorphism
While GWT RPC supports polymorphic parameters and return types, it is best to try to be as specific as possible when defining service interfaces. Increased specificity allows the GWT Compiler to do a better job of optimizing your code and reducing the size of your application.
Does GWT support java.io.Serializable?
Prior to GWT version 1.4, the GWT RPC mechanism only used the IsSerializable marker interface to denote serializable classes. However, as of GWT version 1.4, the GWT RPC system does support limited use of java.io.Serializable but with one specific condition. Please note that this GWT still doesn’t support Java serialization. Support for the java.io.Serializable interface was added to make it easier for developers to use existing code with their GWT applications.
The GWT Compiler generates a serialization policy file (.gwt.rpc file). The serialization policy file contains a whitelist of types that can be serialized. In order to enable java.io.Serializable support, you need to include all the types that your application will send over the wire in the serialization policy whitelist. The serialization policy file must be deployed to your web server as a public resource and needs to be accessible from a RemoteServiceServlet via ServletContext.getResource(). If the serialization whitelist policy file is not deployed properly, your GWT application will run in 1.3.3 compatibility mode and refuse to serialize types implementing java.io.Serializable.
Contact list app screenshot
All the source code provided on this site is distributed under the Apache Open Source License v2.0.
- You can download the full source code used to build the Contact List Application here.
- You can download the WAR file to deploy the Contact List Application here.
Comments and Discussion
If you want to comment on this article, please click here.