Bjartr
Bjartr

Reputation: 502

Sharing GWT class implementation between client and server

Say I have a class implementation in GWT that is more complex than a POJO, but whose methods are still relatively simple i.e. it has some utility methods in addition to its getters and setters such that I can call new Foo() successfully in both client and server code.

My question is this: If I have an instance of such an object, how do I communicate it between a GWT server and client? That is, how do I both transmit its fields while retaining the ability to call the other methods when the object is deserialized?

We are currently using Jackson on the server side and AutoBeans on the client side, but that is strictly limited to POJO classes because AutoBeans come from an interface containing only getters and setters and there's no way to (automatically) pull that data into the more useful object.

For the sake of completeness here's the object in question http://pastebin.com/H2ea0W6A

Upvotes: 1

Views: 578

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

You can actually put logic within your AutoBeans, using @Category:
https://code.google.com/p/google-web-toolkit/wiki/AutoBean#Categories

It however won't work for your setKWHR and getKWHR (at least not if named like property accessors).

It'd probably be easier to move those methods out of the Energy class to leave it as only a DTO.
Alternatively, wrap such a DTO (at least on the client, the DTO being the AutoBean) into another object that could bring the logic (and simply delegate getMMBTU and getDisplayUnit to the wrapped DTO).

Either that or use something like Piriti to do the deserialization on the client-side, instead of AutoBeans; or copy your AutoBean data into an Energy instance.

Upvotes: 2

Related Questions