GWT Object Serialization: Problems and (some) workarounds

I made the very troublesome discovery that different rules apply for Gwt to convert Java objects to JavaScript objects and using Java objects as parameters in Gwt’s remote procedure calls (RPC). Following a list of issues arising when using Gwt RPC (but not in Java->JavaScript conversion).

Immutable Objects (final fields)

Problem:

I have already written about issues concerning immutable objects. final fields are treated as being transient and therewith ignored when serializing and deserializing objects.

Workaround:

Remove the final modifier from any fields you require. This is the only workaround I could find. Even the (possibly deprecated) deRPC does not work with final fields.

Generic Java Objects

Problem:

Gwt RPC does not like fields of the type Object. If an object with a field of the type Object is to be serialized for Gwt RPC, a SerializationException will be thrown.

Workaround:

The only possible workaround I could find is to replace Object with any other class or interface. Especially useful in this regard is the interface java.io.Serializable. This interface is implemented by many fundamental classes such as String and Integer. However, using java.io.Serializable can cause the Gwt compile to significantly increase the size of the JavaScript. Essentially, any class, which implements this interface, must be compiled into JavaScript as it might (or might not) be sent via RPC. A possible mitigation of this problem is to manually exclude classes from being prepared for serialization. This can be achieved by utilizing the rpc-blacklist property. Some examples for setting this property are given below:

<extend-configuration-property name="rpc.blacklist" value="-.*List"/>
<extend-configuration-property name="rpc.blacklist" value="-.*Map"/>
<extend-configuration-property name="rpc.blacklist" value="-.*Collection"/>
<extend-configuration-property name="rpc.blacklist" value="+java.util.HashMap"/>
<extend-configuration-property name="rpc.blacklist" value="-java.util.HashSet"/>
<extend-configuration-property name="rpc.blacklist" value="-java.util.LinkedHashMap"/>
<extend-configuration-property name="rpc.blacklist" value="+java.util.ArrayList"/>
<extend-configuration-property name="rpc.blacklist" value="-com.extjs.*"/> <!-- for GXT -->

Resources

Issue 1833: GWT doesn’t allow Object/Serializable parameter in the serializable class

Issue 4438: GWT Compiler includes unneeded classes in RPC code (+example <extend-configuration-property> definitions to reduce generated code size)

One thought on “GWT Object Serialization: Problems and (some) workarounds

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