Grails命令对象:如何加载request.JSON到它? [英] Grails Command Object: How to load request.JSON into it?

查看:325
本文介绍了Grails命令对象:如何加载request.JSON到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:有没有办法使用request.JSON数据执行自动命令对象绑定?



grails控制器:

  class ProfileCommand {

int id
字符串companyName

static constraints = {
companyName blank:false
id可为空:false
}
$ b $ @覆盖
public String toString(){
returnProfileCommand {id = $ id,companyName ='$ companyName'};


和我的控制器方法签名:



def update(ProfileCommand命令){...}



我如何获得 request.JSON 数据到我的命令对象?



到目前为止,我唯一的方法能够做到的是在 update()方法内手动创建命令对象,传入request.JSON作为构造函数参数:

  def command = new ProfileCommand(request.JSON)

log.debug命令对象内容:$ command

上述调试命令产生:

 命令对象内容:ProfileCommand {id = 1,companyName ='Blub Muckers'} 

这正是我想要的(奥利弗泰恩斯对上述解决方案大肆宣传)。不幸的是,在创建命令后调用 command.validate()会产生以下异常:

  Class org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException 
消息标记[validate]缺少必需属性[form]

我使用的是v2.0.3,uris产生了与w / v2.0.4相同的异常。

更新
在grails邮件列表中,每个Ian Roberts都需要添加 @Validateable 注释到命令类来获得 validate()来工作。谢谢,Ian!

解决方案

我不确定是否有任何配置明智的做自动JSON参数数据绑定;你可能可以做的一件事就是为你的动作编写一个Filter,它接受JSON请求输入,基本上将request.JSON重新映射到根params映射上,理论上允许发生自动数据绑定。



类似于:

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

然后,您可以通过正则表达式/命名约定将此过滤器扩展为任何适用的控制器操作。

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!

解决方案

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.

这篇关于Grails命令对象:如何加载request.JSON到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆