如何在GWT中将任何Serializable对象发送到客户端

时间:2012-03-22 08:23:34

标签: gwt gwt-rpc

想象一下,您希望使用DTO将任何Serializable类发送到GWT应用程序的客户端:

public class MyDTO implements Serializable {

    public Serializable value;

}

此外,任何被用作值的东西都会被检查,如果它在设置之前是Serializable。 GWT会在开发控制台中抛出几个警告:

DEBUG: com.example.app.shared.MyDTO. 
    DEBUG: Analyzing the fields of type 'com.example.app.shared.MyDTO' that qualify for serialization. 
        DEBUG: private java.io.Serializable value. 
            DEBUG: java.io.Serializable. 
                DEBUG: Verifying instantiability. 
                    DEBUG: java.util.ArrayList<? extends java.lang.Object>. 
                        WARN: Checking all subtypes of Object which qualify for serialization. 
                            DEBUG: com.google.gwt.validation.client.impl.PathImpl. 
                                DEBUG: Verifying instantiability. 
                                    DEBUG: com.google.gwt.validation.client.impl.PathImpl. 
                                        DEBUG: Analyzing the fields of type 'com.google.gwt.validation.client.impl.PathImpl' that qualify for serialization. 
                                            WARN: Field 'private final java.util.List<javax.validation.Path.Node> nodes' will not be serialized because it is final.

但是!不幸的是,这会导致GWT在发送到客户端时抛出RPC SerializationException:

com.google.gwt.user.client.rpc.SerializationException: Type 'com.example.app.shared.MyDTO' 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.example.app.shared.MyDTO@577f52ed
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.client.rpc.core.java.util.Collection_CustomFieldSerializerBase.serialize(Collection_CustomFieldSerializerBase.java:44)
    ...

底线: 你如何阻止GWT对Serializable子类型的拟合?

修改

我最终为我需要的每个类创建了一个子类。

1 个答案:

答案 0 :(得分:5)

GWT应该在编译时知道类的类型。所以你需要指定实现Serializable的确切类。

要想克服,我想您可以使用地图将数据作为键值对发送。