1
votes

I have a fairly simple case. I have also simplified the names to make it easier to read (see below).

I have an enum and a user-defined class which uses this enum, as well as a String.

According to GWT:

  • The enum should be serializable.
  • The user-defined class should be serializable because:
  • It directly implements Serializable.
  • It’s non-final, non-transient instance fields are serializable:
  • The enum is serializable.
  • The String is serializable.
  • It has a default (zero argument) constructor.

Despite conforming to these rules, an attempt to use these classes results in the following error:

SEVERE: Exception while dispatching incoming RPC call com.google.gwt.user.client.rpc.SerializationException: Type 'com.company.product.Bar was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = com.company.product.Bar @4c19cc84

Simple case.

GWT bungles it.

Is it broken?

A type is serializable and can be used in a service interface if one of the following is true:

  • The type is primitive, such as char, byte, short, int, long, boolean, float, or double.
  • The type an instance of the String, Date, or a primitive wrapper such as Character, Byte, Short, Integer, Long, Boolean, Float, or Double.
  • The type is an enumeration. Enumeration constants are serialized as a name only; none of the field values are serialized.
  • The type is an array of serializable types (including other serializable arrays).
  • The type is a serializable user-defined class.
  • The type has at least one serializable subclass.
  • The type has a Custom Field Serializer

A user-defined class is serializable if all of the following apply:

  1. It is assignable to IsSerializable or Serializable, either because it directly implements one of these interfaces or because it derives from a superclass that does
  2. All non-final, non-transient instance fields are themselves serializable, and
  3. As of GWT 1.5, it must have a default (zero argument) constructor (with any access modifier) or no constructor at all.

Foo.java

public enum Foo {
 A, B, C;
}

Bar.java

public class Bar implements Serializable {

 private static final long serialVersionUID = 604131643709466885L;
 private Foo foo;
 private String S;

 public Bar() {
 }

 public Foo getFoo() {
     return foo;
 }
 public void setFoo(Foo foo) {
     this.foo = foo;
 }
 public String getS() {
     return S;
 }
 public void setS(String S) {
     this.S = S;
 }
}
2

2 Answers

1
votes

Is your class available on the client and server side?

The package name 'com.company.product.Bar' indicates, that the class might not be in the right package. Normally you have a package structure like this in GWT:

+- root package
   !
   +- client
   !
   +- server
   !
   +- shared
   !  ! 
   !  +- YourDTO.java
   !
   +- YourModuleDescriptor.gwt.xml

Your DTO should be located in the shared package.

0
votes

Type 'com.company.product.Bar was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.

Since you've followed the specific rules it mentioned very clearly, this leaves the other half - something might be wrong with the serialization policy itself. There are two main cases where this can happen - either the policy wasn't present on the server (or is out of date, actually the same thing), or the policy doesn't include that type because it can't be reached by RPC (not on the client sourcepath, explicitly blacklisted, not assignable to one of the expected types - java.lang.Object is cheating).

If the file is outright missing or is out of date, the server will be not send or receive the type it is looking at. If out of date, the client and server have a different idea of what types can go over the wire. Each rpc call contains a reference to the policy file to be used for that call - the client and server need to remain in sync. If on the other hand the object can't be sent because the RPC generator decided that the type wasn't legal to send over the wire, your rpc interface (or referencing type) will need to be changed to properly reference it.

Beyond that, more details will be required - what is your RPC interface, any other log messages on the server, is the *.gwt.rpc file present on the server?