2012-03-22 35 views
4

試想一下,你想要的任何Serializable類發送到您的GWT應用程序的客戶端,使用一個DTO的:如何任何可序列化對象發送到客戶端的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 = [email protected] 
    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從扔一個合適大約序列化亞型?

編輯:

我已經結束了創建爲每個類我需要一個子類。

回答

5

GWT在編譯時應該知道類的類型。所以你需要指定實現Serializable的確切類。

爲了克服,我想你可以使用地圖來發送你的數據作爲鍵值對。

相關問題