How do I add a type to GWT's Serialization Pol

2020-01-27 02:15发布

GWT's serializer has limited java.io.Serializable support, but for security reasons there is a whitelist of types it supports. The documentation I've found, for example this FAQ entry says that any types you want to serialize "must be included in the serialization policy whitelist", and that the list is generated at compile time, but doesn't explain how the compiler decides what goes on the whitelist.

The generated list contains a number of types that are part of the standard library, such as java.lang.String and java.util.HashMap. I get an error when trying to serialize java.sql.Date, which implements the Serializable interface, but is not on the whitelist. How can I add this type to the list?

9条回答
beautiful°
2楼-- · 2020-01-27 02:44

IMHO the simpliest way to access whitelist programmatically is to create a class similar to this:

public class SerializableWhitelist implements IsSerializable {
    String[] dummy1;
    SomeOtherThingsIWishToSerialize dummy2;
}

Then include it in the .client package and reference from the RPC service (so it gets analyzed by the compiler).

I couldn't find a better way to enable tranfer of unparameterized maps, which is obviously what you sometimes need in order to create more generic services...

查看更多
放我归山
3楼-- · 2020-01-27 02:44

to ensure the desired result delete all war/<app>/gwt/*.gwt.rpc

查看更多
祖国的老花朵
4楼-- · 2020-01-27 02:45

Any specific types that you include in your service interface and any types that they reference will be automatically whitelisted, as long as they implement java.io.Serializable, eg:

public String getStringForDates(ArrayList<java.util.Date> dates);

Will result in ArrayList and Date both being included on the whitelist.

It gets trickier if you try and use java.lang.Object instead of specific types:

public Object getObjectForString(String str);

Because the compiler doesn't know what to whitelist. In that case if the objects are not referenced anywhere in your service interface, you have to mark them explicitly with the IsSerializable interface, otherwise it won't let you pass them through the RPC mechanism.

查看更多
【Aperson】
5楼-- · 2020-01-27 02:45

There's a workaround: define a new Dummy class with member fields of all the types that you want to be included in serialization. Then add a method to your RPC interface:

Dummy dummy(Dummy d);

The implementation is just this:

Dummy dummy(Dummy d) { return d; }

And the async interface will have this:

void dummy(Dummy d, AsyncCallback< Dummy> callback);

The GWT compiler will pick this up, and because the Dummy class references those types, it will include them in the white list.

Example Dummy class:

public class Dummy implements IsSerializable {
    private java.sql.Date d;
}
查看更多
贪生不怕死
6楼-- · 2020-01-27 02:46

The whitelist is generated by the GWT compiler and contains all the entries that are designated by the IsSerializable marker interface.

To add a type to the list you just need to make sure that the class implements the IsSerializable interface.

Additionally for serialization to work correctly the class must have a default no arg constructor (constructor can be private if needed). Also if the class is an inner it must be marked as static.

查看更多
相关推荐>>
7楼-- · 2020-01-27 02:46

I found that just putting it in the client package or using it in a dummy service interface was not sufficient as it seemed the system optimized it away.

I found it easiest to create a class that derived from one of the types already used in the service interface and stick it in the client package. Nothing else needed.

public class GWTSerializableTypes extends SomeTypeInServiceInterface implements IsSerializable {
    Long l;
    Double d;
    private GWTSerializableTypes() {}
}
查看更多
登录 后发表回答