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());
}
By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.
The generic collections disable the type-casting and there is no use of type-casting when it is used in generics. The generic collections are type-safe and checked at compile-time. These generic collections allow the datatypes to pass as parameters to classes.
< T > is a conventional letter that stands for "Type", and it refers to the concept of Generics in Java. You can use any letter, but you'll see that 'T' is widely preferred. WHAT DOES GENERIC MEAN? Generic is a way to parameterize a class, method, or interface.
6 Answers. Show activity on this post. Well there's no difference between the first two - they're just using different names for the type parameter ( E or T ). The third isn't a valid declaration - ? is used as a wildcard which is used when providing a type argument, e.g. List<?>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With