gpt4 book ai didi

来自 JSON POST 正文的 Grails 2.5.0 Controller 操作方法参数

转载 作者:行者123 更新时间:2023-12-02 16:00:50 25 4
gpt4 key购买 nike

在 Grails 2.5.0 中,是否可以将 JSON POST 正文中的属性值注入(inject)到不是命令对象的 Controller 操作方法参数中?例如,转换为字符串、原语等。

这在 Grails 2.2.4 中是可能的,但我还没有找到在 2.5.0 中做到这一点的方法。

(我知道查询字符串值可以注入(inject)到 Grails 2.5.0 和 2.2.4 中的 Controller 操作方法参数中)

最佳答案

将请求正文绑定(bind)到命令对象 http://grails.github.io/grails-doc/2.3.0/guide/introduction.html#whatsNew23中的部分.它在 Grails 2.3.x 中有所改变。基本上,如果您尝试访问请求 JSON 两次,您将无法使用它,因为 Grails 在解析数据后关闭请求流并使用它来绑定(bind)任何 CommandObject 或任何域实例(作为命令对象)。

因此,如果您将请求 JSON 传递给操作,请说支持:{"foo": "bar"}你正在尝试这样做:

class SomeController {

def test(String foo) {
println foo // Will be null
println request.JSON.foo // Will be "bar"
}
}

相反,任何域类绑定(bind)现在都可以工作:
class MyDomainClass {

String foo
}

并修改 Controller Action :
class SomeController {

def test(MyDomainClass domainInstance) {
println domainInstance.foo // Will be "bar"
println request.JSON // Will be null since request stream is closed and binded to the domainInstance
}
}

关于来自 JSON POST 正文的 Grails 2.5.0 Controller 操作方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com