GWT RPC serialization and circular references - chicken and egg problem
I'm using GWT 2.1.1 and client-server communication using the AsyncService (not RequestFactory). I have an object that returns a "fund" object. The fund has a reference to a "distributor" object which itself has a collection of "fund" objects.
In one scenario, I'm returning from the server, fund "foo" who's distributor reference has "foo", "bar" and "joe" funds. This is a common business scenario. When deserializing on the client side, I get an error because the "foo" reference in the collection of funds in distributor ends up with no values populated. In particular, the fund-id (string) is not populated and this is used in the hashcode implementation. During client-side deserialization, "bar" and "joe" funds get deserialized properly, but foo fails, i.e., gets deserialized with only a few properties.
What is going on here is that when "foo" is getting deserialized, it has a few properties deserialized and then gwt attempts to deserialize the distributor. Foo's fund-id property has not yet been deserialized. So when distributor's collection of funds is being deserialized, and "foo" in encountered, the deserializer gives back a reference to the currently in-deserializing "foo" which doesn't have the fund-id. So when GWT attempts to add it to the Set (开发者_高级运维HashSet) in distributor, the hashcode implementation fails.
So my question is, in such cases, is there any way to force GWT to serialize certain properties (in this case fund-id which hashCode depends on) first?
Found that this is actually a known issue. For anyone else who runs across this, and finds this question through a search:
http://code.google.com/p/google-web-toolkit/issues/detail?id=3577
I'll post info about how to implement a custom serializer once I read up on it so folks have a fuller picture. I invite others to do so as well.
精彩评论