Suppose I have two models: Foo and Bar. Bar is already in the database with an instance with ID 123. In building out a REST API, I want to allow the API to accept a JSON representation of Foo, including the reference to the "123" instance of Bar.
This would obviously be bad:
{ fooProp1: "...", fooProp2: "...", bar: { id: 123, barProp1: "..." } }
Instead I'd prefer something like:
{ fooProp1: "...", fooProp2: "...", bar: 123 }
Of course, when I run this through Json.fromJson(json, Foo.class), it blows up complaining that "123" can't be converted to Bar. I have used Jackson directly before and what I'd normally do is register a converter that would do the mapping for me and call Bar.find.byId() on my behalf. So two questions:
1) Is this possible?
2) Is it a best practice? If not, what is the recommended way to do this kind of stuff.
Thanks!
Patrick