【问题标题】:How to send any Serializable object to the client side in GWT如何在 GWT 中将任何可序列化对象发送到客户端
【发布时间】:2012-03-22 08:23:33
【问题描述】:

假设您想使用 DTO 将任何 Serializable 类发送到 GWT 应用程序的客户端:

public class MyDTO implements Serializable {

    public Serializable value;

}

此外,任何被用作值的东西都将在设置之前检查它是否可序列化。 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 子类型提出异议?

编辑:

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

【问题讨论】:

    标签: gwt gwt-rpc


    【解决方案1】:

    GWT 应该在编译时知道类的类型。因此,您需要指定实现 Serializable 的确切类。

    为了克服,我认为您可以使用映射将数据作为键值对发送。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多