SerializationException: type not included in serializable type set

限于喜欢 提交于 2020-01-10 03:31:07

问题


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?


回答1:


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.




回答2:


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




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




回答4:


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.




回答5:


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




回答6:


When your class has JDO annotations, then this fixed it for me (in addition to the points in bspoel's answer) : https://stackoverflow.com/a/4826778/1099376



来源:https://stackoverflow.com/questions/5210696/serializationexception-type-not-included-in-serializable-type-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!