开发者

Using JSON to create an object in Groovy/Grails

开发者 https://www.devze.com 2023-04-03 16:15 出处:网络
I have a Groovy/Grails website that is being used to send data to Android clients via JSON. I have created both the Android client and the Groovy/Grails website; and they can output the same objects i

I have a Groovy/Grails website that is being used to send data to Android clients via JSON. I have created both the Android client and the Groovy/Grails website; and they can output the same objects in JSON.

I can successfully create the respective objects in Android by mapping the JSON output to Java objects, however I was wondering if it's possible to use the JSON output to create a new domain object in Groovy/Grails? Is there a way of passing the JSON output to a controller action so that object will be created?

Here is an example of the JSON that I'd 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"
        }
    }
    ]
}

The "class" matches to the Domain I'd like to save to, the ID is the ID of the Domain, then each item within the businessCardDesigns and emailAddrs needs to be saved using similar m开发者_如何学编程ethods (in the Domain the businessCardDesigns and emailAddrs are ArrayLists). Many thanks in advance!

SOLUTION:

@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);
}


Using the built-in Grails JSON converter makes this easier than the 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)
    }
}

The other benefits are:

  • There's no need to muck around in any config files
  • The resulting JSON object can be manipulated, if necessary, before assigning properties
  • It's far clearer in the code what's happening (we're parsing a JSON object and setting the properties on the Person entity)


I know you already accepted an answer but if I'm reading your question right, there's a built in "Grails" way to do this.

Create an entry for your action in URLMappings.groovy and turn on request parsing. For example, I create RESTful mappings like so:

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

And then in you controller

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


this might work for you

http://static.springsource.org/spring-roo/reference/html/base-json.html

0

精彩评论

暂无评论...
验证码 换一张
取 消