GWT Serialization

GWT RPC uses Serialization to facilitate async communication between server and client.

All GWT RPC Services extend RemoteServiceSrvlet (RSS) instead of HttpServlet.  RSS automatically takes care of serialization (in addition to calling appropriate service method).

RPC method params and return values needs to be serializable.

GWT Serialization is different from java.io.serialization and the following are rules to user classes in RPC:

  • primitives
  • String, Date, or a primitive wrapper
  • Enumeration
  • array of serializable types (including other serializable arrays).
  • user-defined serializable class.
  • The type has at least one serializable subclass.

User-Defined GWT Serializable Class

  • assignable to IsSerializable or Serializable.
  • All non-final, non-transient instance fields are themselves serializable, and
  • Prior to GWT 1.5, it must have a public default (zero argument) constructor or no constructor at all.
  • As of GWT 1.5, it must have a default (zero argument) constructor (with any access modifier) or no constructor at all.

The transient keyword is honored, so values in transient fields are not exchanged during RPCs.So are fields that are declared as final and hence advisable to be marked transient as well.

GWT RPC serialization supports polymorphism with params and return types.

Making collections serializable in GWT

Prior to GWT 1.4 @gwt.typeArgs annotation is used

in GWT 1.5 parameterized types are used.

Adding an object that doesn’t satisfy type constraint (of course in 1.4) leads to undefined behavior.

  • For RPC interfaces you need to annotate the method param’s powith field names and return types

           Ex:
           @gwt.typeArgs idList <java.lang.Integer>
           @gwt.typeArgs <java.lang.String>
           List populateList(List idList)

  • For DataTypes (DTO) you need to annotate the fields

GWT Serialization File Name:

*.gwt.rpc

Custom Field Serializer for GWT RPC

To custom serialize com.my.DTO creeate a class named com.my.DTO_CustomFieldSerializer and implement the following methods in the class:

    public static void serialize(SerializationStreamWriter streamWriter, MyObject myObject)
        throws SerializationException {}

    public static void deserialize(SerializationStreamReader streamReader, MyObject myObject)
        throws SerializationException {}

    //This method is optional
    public static DTO instantiate(SerializationStreamReader streamReader)
        throws SerializationException {}

More about custom serialization: http://code.google.com/p/wogwt/wiki/CustomFieldSerializer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s