GWT和泛型

ram*_*lla 7 java generics gwt

使用RPC和Java Generics发送对象列表时,我们遇到SerializationException错误.

我正在创建此小部件以显示错误:

public class Test<T> {

    ListDataProvider<T> ldp = new ListDataProvider<T>();

    public void setItems(List<T> list){
        for(T t :list){
            ldp.getList().add(t);
        }
    }

    public List<T> getItems(){
        return ldp.getList();

    }

}
Run Code Online (Sandbox Code Playgroud)

这是用于创建Test小部件并传递POJO列表的代码(其中ExporterFormKey是POJO对象)

List<ExporterFormKey> list = new ArrayList<ExporterFormKey>();
ExporterFormKey key = new ExporterFormKey();
key.setKey("key1");
list.add(key);

Test<ExporterFormKey> test = new Test<ExporterFormKey>();
test.setItems(list);
Run Code Online (Sandbox Code Playgroud)

最后,下一个代码抛出一个SerializationException:

service.sendList(test.getList(), new AsyncCallback...);
Run Code Online (Sandbox Code Playgroud)

虽然下一个很好:

service.sendList(list, new AsyncCallback...);
Run Code Online (Sandbox Code Playgroud)

- - -编辑 - -

我发现做下一个代码也有效

List<ExporterFormKey> newList = new ArrayList<ExporterFormKey>();
newList.add(test.getItems().get(0));
service.sendList(newList , new AsyncCallback...);
Run Code Online (Sandbox Code Playgroud)

或者这也有效

List<ExporterFormKey> newList = new ArrayList<ExporterFormKey>(test.getItems());
Run Code Online (Sandbox Code Playgroud)

我也发现测试工作的这个变化!

public List<T> getItems(){
    return new ArrayList<T>(ldp.getList());
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 1

http://blog.rubiconred.com/2011/04/gwt-serializationexception-on-rpc-call.html

正如 izaera 建议的那样,ListDataProvider 使用不可序列化的列表实现(ListWrapper),它不能直接通过线路发送。

按照您在帖子中的建议,将 ListDataProvider 的 getList() 方法的响应包装到新的 ArrayList 中是解决该问题的最简单方法。