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!