SerializationException: type not included in serializable type set
Asked Answered
B

6

32

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?

Brisco answered 6/3, 2011 at 13:14 Comment(1)
It means that the return type of the Async call is not serialized. to sort this, implement java.io.seriablizable interface in the return type class.Solarize
B
69

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.

Brisco answered 6/3, 2011 at 13:17 Comment(3)
amazing! I had not added a default constructor... did not know the purpose of that constructor for Serialized types.... but I my Boss explained it to me after fixing it.... --Thanks again :)Hepplewhite
You are a life saver.. just got some more info at the link isolasoftware.it/2011/03/22/gwt-serialization-policy-errorBobette
Is it possible that this does not work for enum types?Abut
E
4

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

Embellishment answered 1/11, 2012 at 20:5 Comment(1)
Moving the class outside is another option.Brookbrooke
B
3

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...

Bomke answered 20/11, 2012 at 22:44 Comment(1)
Thank you! I had a similar issue. I had a signature that looked like Collection<X> foo(); I replaced Collection by an implementation (ArrayList) and it worked.Erund
A
2

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.

Assimilate answered 20/3, 2013 at 3:11 Comment(0)
P
1

Looks like this question is very similar to what IsSerializable or not in GWT?, see more links to related documentation there.

Pantile answered 10/8, 2011 at 18:56 Comment(0)
K
0

When your class has JDO annotations, then this fixed it for me (in addition to the points in bspoel's answer) : https://mcmap.net/q/454186/-sending-persisted-jdo-instances-over-gwt-rpc

Kathe answered 27/3, 2013 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.