我在GWT应用程序的客户端中有一个枚举,当我尝试运行它与序列化问题相关时,我收到异常.我做错了吗我读过这个枚举是由GWT支持的,我使用的是最后一个版本.
枚举:
public enum AnEnum implements Serializable { ITEM_A("Item a description"),ITEM_B("Item b description"); private String description; private AnEnum(String description) { this.description = description; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
例外:
Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeWithCustomSerializer(ServerSerializationStreamWriter.java:742) ... 47 more Caused by: com.google.gwt.user.client.rpc.SerializationException: Type '(...).client.(...).AnEnum' 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 = ITEM_A at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:610) at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:129) at com.google.gwt.user.client.rpc.core.java.util.Collection_CustomFieldSerializerBase.serialize(Collection_CustomFieldSerializerBase.java:43) at com.google.gwt.user.client.rpc.core.java.util.LinkedList_CustomFieldSerializer.serialize(LinkedList_CustomFieldSerializer.java:36) ... 52 more
解决方法
添加IsSerializable接口,一个默认范围的无参数构造函数,并确保其在gwt.xml文件中的源代码中列出的路径之一. < source path =“client”>
我真的认为第三个建议是问题;我记得以前有这个问题,这是因为我在源路径之外有一个dto.
您可以有多个源代码.
<source path="common" /> <source path="client" />
一种模式是将持久化对象直接放在com.mysite.common下,以及通过com.mysite.common.dto通过电线传输的持久化项的混搭,当然客户端gui代码在客户端.
package com.mysite.client; import java.io.Serializable; import com.google.gwt.user.client.rpc.IsSerializable; public enum AnEnum implements Serializable,IsSerializable { ITEM_A("Item a description"),ITEM_B("Item b description"); private String description; AnEnum() { } AnEnum(String description) { this.description = description; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }