I have a Groovy / Grails website that is used to send data to Android clients via JSON. I created both the Android client and the Groovy / Grails website; and they can output the same objects in JSON.
I can successfully create the corresponding objects in Android by matching the JSON output with the Java objects, however I was wondering if it is possible to use the JSON output to create a new domain object in Groovy / Grails? Is there a way to pass JSON output to a controller action so that the object is created?
Here is an example JSON I would like to send:
{ "class":"org.icc.callrz.BusinessCard.BusinessCard", "id":1, "businessCardDesigns":[], "emailAddrs":[ { "class":"org.icc.callrz.BusinessCard.EmailAddress", "id":1, "address":" chris@krslynx.com ", "businessCard":{ "_ref":"../..", "class":"org.icc.callrz.BusinessCard.BusinessCard" }, "index":0, "type":{ "enumType":"org.icc.callrz.BusinessCard.EmailAddress$EmailAddressType", "name":"H" } }, { "class":"org.icc.callrz.BusinessCard.EmailAddress", "id":2, "address":" cb@i-cc.cc ", "businessCard":{ "_ref":"../..", "class":"org.icc.callrz.BusinessCard.BusinessCard" }, "index":1, "type":{ "enumType":"org.icc.callrz.BusinessCard.EmailAddress$EmailAddressType", "name":"W" } } ] }
"Class" corresponds to the domain that I would like to save, the identifier is the domain identifier, then each element in businessCardDesigns and emailAddrs should be saved using similar methods (in the domain businessCardDesigns and emailAddrs are ArrayLists). Thank you very much in advance!
DECISION:
@RequestMapping(method = RequestMethod.POST, headers = "Accept=application/json") public ResponseEntity<String> createFromJson(@RequestBody String json) { Owner.fromJsonToOwner(json).persist(); return new ResponseEntity<String>(HttpStatus.CREATED); }