Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send any Serializable object to the client side in GWT

Tags:

gwt

gwt-rpc

Imagine that you want to send any Serializable class to the client side of your GWT application, with the use of a DTO:

public class MyDTO implements Serializable {

    public Serializable value;

}

Also, anything that gets used as value will be checked if it's Serializable before it is set. GWT does throw a couple of warnings in the dev console:

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.

But! Unfortunately, this results in GWT throwing an RPC SerializationException when it gets sent through to the client side:

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)
    ...

Bottomline: How do you prevent GWT from throwing a fit about Serializable subtypes?

EDIT:

I've ended up creating a subclass for each class I needed.

like image 602
levivanzele Avatar asked Mar 22 '12 08:03

levivanzele


1 Answers

GWT should know the type of class in compile time. So you need to specify exact class which implements Serializable.

To overcome, I think you may use a map to send your data as key-value pairs.

like image 98
Alexey A. Avatar answered Sep 24 '22 19:09

Alexey A.