ramon_salla
ramon_salla

Reputation: 1607

GWT and Generics

We are having a SerializationException error when sending a list of objects using RPC and Java Generics.

I'm creating this widget to show the error:

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();

    }

}

This is the code for creating the Test widget and passing a list of POJOs (where ExporterFormKey is the POJO object)

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

At the end the next code throws a SerializationException:

service.sendList(test.getList(), new AsyncCallback...);

While the next one does fine:

service.sendList(list, new AsyncCallback...);

-----Edit----

I found that doing the next code also works

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

Or this also works

List<ExporterFormKey> newList = new ArrayList<ExporterFormKey>(test.getItems());

I also found this change on Test works!

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

Upvotes: 7

Views: 1894

Answers (1)

Josh
Josh

Reputation: 838

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

As izaera suggested the ListDataProvider uses a non-serializable list implementation (ListWrapper) which cannot be sent directly across the wire.

Wrapping the response from ListDataProvider's getList() method into a new ArrayList as you have suggested in your post is the simplest way to workaround the issue.

Upvotes: 1

Related Questions