Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 970
John Gordon Avatar asked May 31 '12 13:05

John Gordon


3 Answers

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.

like image 120
Aquatoad Avatar answered Oct 18 '22 20:10

Aquatoad


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).

like image 4
Dmitry Kurinskiy Avatar answered Oct 18 '22 21:10

Dmitry Kurinskiy


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

like image 1
Ian Roberts Avatar answered Oct 18 '22 19:10

Ian Roberts