GWT and Generics

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

I create this widget to show an 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 a Test widget and passing a POJO list (where ExporterFormKey is a 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 of the following code, a SerializationException is thrown:

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

While the following does well:

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

----- Edit ----

I found that executing the following code also works

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

Or it also works

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

I also found this change in test work!

 public List<T> getItems(){ return new ArrayList<T>(ldp.getList()); } 
+7
source share
1 answer

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

Since izaera suggested that ListDataProvider uses a non-serializable implementation of the List (ListWrapper), which cannot be sent directly through the wire.

Wrapping the response from the ListLataProvider getList () method to a new ArrayList, as you suggested in your post, is the easiest way to work around the problem.

+1
source

All Articles