GWT serialization issue
I am having a heck of a time returning an ArrayList of objects that implement IsSerializable via RPC. The IsSerializable pojo contains one variable, a String, and has a 0 parameter constructor. I have removed the .gwt.rpc file from my war and still I get:
com.google.gwt.user.client.rpc.SerializationException: Type 'com.test.myApp.client.model.Test' was not included in the set of types which can be serialized by this Seriali开发者_运维知识库zationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = com.test.myApp.client.model.Test@17a9692
at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:610)
I am using GWT 2.0.2 with jdk 1.6.0_18.
Any ideas on what might be going on or what I am doing wrong?
Here is the code for the Test class and the remote method is returning ArrayList. I even modified the code for it to just return one instance of Test with the same result: the exception above.
package com.test.myApp.client.model;
import com.google.gwt.user.client.rpc.IsSerializable;
public class Test implements IsSerializable{
private String s;
public Test() {}
public Test(String s) {
this.s = s;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
Greatly appreciate the help! Eddy
This may sound silly, but do you have a default constructor for Test in your code, and not just on the version of the code you posted here? I encountered this same error, googled it, tried several of the suggestions here like clean/rebuild, add serialVersionUID, none of it worked. Then I searched more and found a suggestion to make sure your class that you're trying to serialize has a default constructor. Mine didn't, and as soon as I added one, it worked. I don't know why that fixed it, but it did.
I also added an empty constructor to my class that was having the problem, and it worked fine.
The remote method needs to return ArrayList<Test>
, not just ArrayList
, so that GWT understands that instances of Test
will need to be serialised.
I'm used to using the Serializable interface declaration so this answer may not apply.
Eclipse always prompts me to create a serialVersionUID member for any Serializable class.
private static final long serialVersionUID = 2388319784164372900L;
One thought that just hit me, something is out of sync in your builds. I suggest you do two things: a) Project | Clean, and then b) GWT Compile Project.
I also had the same issue with ArrayList. I just added an empty constructor and it worked. (My class already implemented Serializable)
or its Class object could not be loaded
Is com/test/myApp/client/model/Test.class included in your war?
These are the cases that will cause GWT serialization to throw an error:
1) Your class does not implement Serializable or IsSerializable. 2) No default constructor 3) GWT is unable to add your class to the "whitelist" because a static analysis of the code does not show that your class will be included in RPC. For such cases, I create a "WhiteList" action/result RPC pair (following the command model) and ensure that the action object has members of the relevant classes that are failing. Sometimes you may find that an object is returned as part of an RPC call in a non-generified collection and GWT has no clue to include it. In this case also, adding it to the "WhiteList" action class helps.
I notice that your class is in the "com.test.myApp.client.mode" package, suggesting that it is a "test" class (and not be part of your expected deployment).
Is your gwt module file (myApp.get.xml) actually in com.test.myApp?
If so, you have chosen your package names poorly. Avoid putting "test" in your main package names - convention dictates this means it's a test package.
If not, try moving your class to a "main" package, eg "com.mycompany.myApp.client.mode" (assuming your get module file is in com.mycompany.myApp).
精彩评论