Grails Command Object: How to load request.JSON in

2019-04-19 10:38发布

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!

3条回答
三岁会撩人
2楼-- · 2019-04-19 11:05

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.

查看更多
何必那么认真
3楼-- · 2019-04-19 11:12

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

查看更多
等我变得足够好
4楼-- · 2019-04-19 11:15

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

查看更多
登录 后发表回答