【发布时间】:2013-11-22 03:33:58
【问题描述】:
我有一个相当简单的案例。我还简化了名称以使其更易于阅读(见下文)。
我有一个枚举和一个使用该枚举的用户定义类,以及一个字符串。
根据 GWT:
- 枚举应该是可序列化的。
- 用户定义的类应该是可序列化的,因为:
- 直接实现Serializable。
- 它的非最终、非瞬态实例字段是可序列化的:
- 枚举是可序列化的。
- 字符串是可序列化的。
- 它有一个默认(零参数)构造函数。
尽管符合这些规则,但尝试使用这些类会导致以下错误:
SEVERE: Exception while dispatching incoming RPC call com.google.gwt.user.client.rpc.SerializationException: Type 'com.company.product.Bar 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 = com.company.product.Bar @4c19cc84
简单案例。
GWT 搞砸了。
它坏了吗?
如果满足以下条件之一,则类型是可序列化的并且可以在服务接口中使用:
- 类型是原始类型,例如 char、byte、short、int、long、boolean、float 或 double。
- 类型是 String、Date 或原始包装器(例如 Character、Byte、Short、Integer、Long、Boolean、Float 或 Double)的实例。
- 类型是枚举。枚举常量仅作为名称序列化;没有任何字段值被序列化。
- 类型是可序列化类型的数组(包括其他可序列化数组)。
- 类型是可序列化的用户定义类。
- 该类型至少有一个可序列化的子类。
- 该类型具有自定义字段序列化器
如果满足以下所有条件,则用户定义的类是可序列化的:
- 它可以分配给 IsSerializable 或 Serializable,要么是因为它直接实现了这些接口之一,要么是因为它派生自具有此功能的超类
- 所有非最终、非瞬态的实例字段本身都是可序列化的,并且
- 从 GWT 1.5 开始,它必须有一个默认(零参数)构造函数(带有任何访问修饰符)或根本没有构造函数。
Foo.java
public enum Foo {
A, B, C;
}
Bar.java
public class Bar implements Serializable {
private static final long serialVersionUID = 604131643709466885L;
private Foo foo;
private String S;
public Bar() {
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
public String getS() {
return S;
}
public void setS(String S) {
this.S = S;
}
}
【问题讨论】:
标签: gwt serialization gwt-rpc