Grails将请求作为JSON发送并在控制器中解析

Fer*_*deh 6 grails json request grails-2.0

我想以JSON的形式发送请求,在我的控制器中我想解析这个JSON并获取我想要的参数.例如,这是请求:

{"param1":"val1"}
Run Code Online (Sandbox Code Playgroud)

我想解析这个请求并获得"param1"值.我使用了request.JSON,但仍然是null.有没有其他方法可以解决这个问题?

谢谢,

Tom*_*ski 3

在你的 UrlMappings 中设置它,如下所示:

static mappings = {
    "/rest/myAction" (controller: "myController", action: "myAction", parseRequest: true)
}
Run Code Online (Sandbox Code Playgroud)

在最新的 Grails 指南中搜索 parseRequest 。

然后用curl验证它是否可以正常工作:

curl --data '{"param1":"value1"}' --header "Content-Type: application/json" http://yourhost:8080/rest/myAction
Run Code Online (Sandbox Code Playgroud)