Grails Command Object: How to load request.JSON into it?
Asked Answered
W

3

12

Question: is there a way to do automatic command object binding with request.JSON data?

Given this simple Command object in my grails controller:

class ProfileCommand{

int id
String companyName

static constraints = {
    companyName blank: false
    id nullable: false
}

@Override
public String toString() {
    return "ProfileCommand{id=$id, companyName='$companyName'}";
}
}

and my controller method signature of:

def update(ProfileCommand command) {...}

How can I get request.JSON data into my command object?

So far, the only way I've been able to do it is to create the command object manually within the update() method, passing in request.JSON as the constructor argument:

    def command = new ProfileCommand(request.JSON)

    log.debug "Command object contents: $command"

The above debug command produces:

Command object contents: ProfileCommand{id=1, companyName='Blub Muckers'}

Which is exactly what I want (A big shout-out to Oliver Tynes for the above solution). Unfortunately, calling command.validate() after I create the command produces the following exception:

Class org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message Tag [validate] is missing required attribute [form]

I'm using v2.0.3, uris produced the same exception w/ v2.0.4.

UPDATE Per Ian Roberts on the grails mailing list, you need to add the @Validateable annotation to the command class to get validate() to work. Thanks, Ian!

Wile answered 31/5, 2012 at 13:16 Comment(0)
D
7

I'm not sure if there is anything configuration-wise to do automatic JSON parameter data binding; one thing you might be able to do is to write a Filter for your actions that take JSON request input that basically remaps request.JSON directly onto the root params map, which should in theory allow the automatic data binding to take place.

something like:

class JSONParamMapFilters {
  def filters = {
     before = {
        remapFilter(controller:'yourController', action:'update') {
           request.JSON.each { k,v ->
               params[k] = v
           }
        }
     }
  }
}

You could then extend this filter via regex/naming conventions to any applicable controller actions.

Dermatosis answered 31/5, 2012 at 13:50 Comment(2)
This would work. I'll mark it as the answer unless anyone else has another suggestion. Thanks, Aquatoad!Wile
Glad to help. I'll remove the bit that's fixed by the @Validateable annotation as well.Dermatosis
S
4

You should use parseRequest=true in UrlMappings.groovy. E.g.:

"/user/$id/$action?"(controller: "userProfile", parseRequest: true) {
    constraints {
        id matches: /^[0-9]+$/
    }
}

Then you may use params variable or bind json to a command object in a method arguments:

def index(MyCommand command){...}

Both should work. But in some cases it looses some information from json (binding to maps).

Seda answered 28/1, 2013 at 8:22 Comment(1)
Just FYI for readers on grails 2.3, parseRequest now does excatly nothing, you need to handle JSON manually.Alsace
W
1

If you're not using the command object as a parameter to any controller actions then Grails won't enhance it automatically with a validate method. You need to annotate the class with @Validateable to tell Grails it should be enhanced.

http://grails.org/doc/latest/guide/validation.html#validationNonDomainAndCommandObjectClasses

When answered 31/5, 2012 at 15:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.