关于java:GWT类:用于防止字段序列化的注释?

 2021-04-27 

GWT classes: an annotation to prevent serialization of a field?

在GWT 1.7中,我有一个类用于在服务器端构造对象,然后在客户端(浏览器)端使用。

在客户端,我想缓存一个服务(在本例中为NumberFormat)。这将在仅客户端方法中延迟初始化,并存储为字段对象。

问题是Java(1.6)生成工具将此字段解释为需要在服务器端进行序列化(即使从未在服务器上访问过)。

我可以将对象package在一些处理方客户端中,但是我宁愿指定一个注释来指示该字段将不再需要序列化以进行RPC传输。

1
2
3
4
5
6
7
8
9
10
11
public class myCrossDomainObject {

  private int someSerializedField;
  private string anotherSerializedField;

  @SomeAnnotationIKnowNotWhat(..)
  private NumberFormat numberFormatterDontSerializeMe;

  // rest of class ......

}


尝试

1
private transient NumberFormat numberFormatterDontSerializeMe;


@GwtTransient

This annotation means the same thing as the transient keyword, but it
is ignored by all serialization systems other than GWT's. Usually the
transient keyword should be used in preference to this annotation.
However, for types used with multiple serialization systems, it can be
useful.
Note that GWT will actually accept any annotation named
GwtTransient for this purpose. This is done to allow libraries to
support GWT serialization without creating a direct dependency on the
GWT distribution.

http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/client/rpc/GwtTransient.html