Using JSON to create an object in Groovy / Grails

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); } 
+6
source share
3 answers

Using the built-in Grails JSON converter makes this easier than other answers, in my opinion:

 import grails.converters.JSON class PersonController { def save = { def person = new Person(JSON.parse(params.person)) person.save(flush:true) } } 

Other benefits:

  • In any configuration files do not need to guess.
  • As a result, the JSON object can be processed, if necessary, before assigning properties
  • The code is much clearer what is happening (we parse the JSON object and set the properties of the Person object)
+12
source

I know you already accepted the answer, but if I read your question correctly, there is a built-in "Grails" way to do this.

Create an entry for your action in URLMappings.groovy and enable query parsing. For example, I create RESTful mappings as follows:

 "/api/bizCard/save"(controller: "businessCard", parseRequest: true) { action = [POST: "save"] } 

And then in your controller

 def save = { def businessCardInstance = new BusinessCard(params.businessCard) .... businessCardInstance.save(flush:true) } 
+6
source

Source: https://habr.com/ru/post/896999/


All Articles