Wednesday, September 15, 2010

REST XML parsing with Grails 1.3.4

We encountered an issue with Grails 1.3.4 where we found that the XML packet was not parsed and added to the params variable in the controller.

The grails manual has a section which describes how to do REST web services. We followed the instructions but we weren't able to get the automatic data binding to our domain class to work. A fix seems to be on the way but in the mean time, the code below provides a workaround for the issue:

private populateParamsFromXML(xml,map) {              
        def rootName = xml.name()
        //check if params is already populated
        if(map[rootName])
            return
        def xmlMap = [:]
        map[rootName] = xmlMap          
        map = xmlMap
        for (child in xml.children()) {
            // one-to-ones have ids
            if (child.@id.text()) {
                map["${child.name()}.id"] = child.@id.text()
                def childMap = [:]
                def key 
                map[child.name()] = childMap
                populateParamsFromXML(child, childMap)
            }
            else {
                map[child.name()] = child.text()
            }
        }
    }

It should be called in a controller action before the params variable is used to populate a domain class.

populateParamsFromXML(request.XML,params)

I hope you found the post useful. You can subscribe via email or subscribe via a feed reader to get relevant updates from this blog. Have a nice day.

6 comments:

  1. This is documented in the manual, see http://www.grails.org/doc/latest/guide/2.%20Getting%20Started.html#2.2%20Upgrading%20from%20previous%20versions%20of%20Grails :
    "Incoming XML requests are now no longer automatically parsed. To enable parsing of REST requests you can do so using the parseRequest argument inside a URL mapping"

    ReplyDelete
  2. Hi Lari Hotari,

    I followed this entry in the users guide: http://grails.org/doc/1.3.4/guide/13.%20Web%20Services.html#13.1%20REST

    And yes, I am aware of the parseRequest argument and yes, i tried it and it did NOT work. I'll upload a sample project somewhere if you'd like to see what I did. I also found an entry in 1.3.5 release notes pertaining to this issue and that its already fixed.

    ReplyDelete
  3. Has been fixed in Grails 1.3.5+, parseRequest:true still required though. See http://jira.grails.org/browse/GRAILS-6614

    ReplyDelete
  4. idn poker apk The overall interest with Texas Holdem poker keeps on developing. The accomplishment of youthful poker players at the World Series of Poker has grabbed the eye of youngsters around the globe.download idn poker

    ReplyDelete
  5. They can check the preferences of every customer, check and save their data and know their requests in simply an issue of a single tick.make money on TikTok

    ReplyDelete
  6. The finest US casinos on-line allow you to strive Demo Play slots with no financial risk concerned. That means you'll be able to|you probably can} play free on-line slots with out committing a penny to make sure you|ensure you|be 빅카지노 positive to} like the game. Resorts Casino options an amazing loyalty program with loads of advantages for its gamers. Among slots and jackpots, gamblers can discover massive selection of slingo and reside supplier video games. Not all states with legal on-line casinos have launched Live Dealer video games but, although the listing will be rising. We advocate checking our individual on line casino state pages for a full breakdown of what’s obtainable in your area.

    ReplyDelete