Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerializationException: type not included in serializable type set

In my Google Web Toolkit project, I got the following error:

com.google.gwt.user.client.rpc.SerializationException: Type ‘your.class.Type’ 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.

What are the possible causes of this error?

like image 594
bspoel Avatar asked Mar 06 '11 13:03

bspoel


4 Answers

GWT keeps track of a set of types which can be serialized and sent to the client. your.class.Type apparently was not on this list. Lists like this are stored in .gwt.rpc files. These lists are generated, so editing these lists is probably useless. How these lists are generated is a bit unclear, but you can try the following things:

  • Make sure your.class.Type implements java.io.Serializable
  • Make sure your.class.Type has a public no-args constructor
  • Make sure the members of your.class.Type do the same

  • Check if your program does not contain collections of a non-serializable type, e.g. ArrayList<Object>. If such a collection contains your.class.Type and is serialized, this error will occur.

  • Make your.class.Type implement IsSerializable. This marker interface was specifically meant for classes that should be sent to the client. This didn't work for me, but my class also implemented Serializable, so maybe both interfaces don't work well together.

  • Another option is to create a dummy class with your.class.Type as a member, and add a method to your RPC interface that gets and returns the dummy. This forces the GWT compiler to add the dummy class and its members to the serialization whitelist.

like image 138
bspoel Avatar answered Nov 10 '22 09:11

bspoel


I'll also add that if you want to use a nested class, use a static member class. I.e.,

public class Pojo {
    public static class Insider {
    }
}

Nonstatic member classes get the SerializationException in GWT 2.4

like image 36
RodK Avatar answered Nov 10 '22 08:11

RodK


I had the same issue in a RemoteService like this

public List<X> getX(...);

where X is an interface. The only implementation did conform to the rules, i.e. implements Serializable or IsSerializable, has a default constructor, and all its (non-transient and non-final) fields follow those rules as well.

But I kept getting that SerializationException until I changed the result type from List to X[], so

public X[] getX(...);

worked. Interestingly, the only argument being a List, Y being an interface, was no problem at all...

like image 3
Pat Avatar answered Nov 10 '22 10:11

Pat


I have run into this problem, and if you per chance are using JPA or Hibernate, this can be a result of trying to return the query object and not creating a new object and copying your relavant fields into that new object. Check the following out, which I saw in a google group.

     @SuppressWarnings("unchecked") 
    public static List<Article> getForUser(User user) 
    { 
            List<Article> articles = null; 
            PersistenceManager pm = PMF.get().getPersistenceManager(); 
            try 
            { 
                    Query query = pm.newQuery(Article.class); 
                    query.setFilter("email == emailParam"); 
                    query.setOrdering("timeStamp desc"); 
                    query.declareParameters("String emailParam"); 
                    List<Article> results = (List<Article>) query.execute(user.getEmail 
     ()); 
                    articles = new ArrayList<Article>(); 
                    for (Article a : results) 
                    { 
                            a.getEmail(); 
                            articles.add(a); 
                    } 
            } 
            finally 
            { 
                    pm.close(); 
            } 
            return articles; 
    } 

this helped me out a lot, hopefully it points others in the right direction.

like image 2
rjdamore Avatar answered Nov 10 '22 10:11

rjdamore