当GETting grails控制器动作与空体时,Json解析错误

时间:2011-04-06 12:29:04

标签: json http grails grails-controller

当我通过使用.json文件扩展名(例如http://localhost:8080/myapp/mycontroller/myaction.json)向我的grails 1.3.7控制器发送带有空主体的GET请求时,我得到一个请求解析异常,似乎grails正在尝试解析我的空身到JSON。如果我将相同的请求发送到相同的操作但没有.json扩展名,我没有任何错误。

如何摆脱这个错误?

2 个答案:

答案 0 :(得分:1)

我最好的做法是在URL映射中使用单独的子句,并确保对于GETtish请求,parseRequest设置为false,即

static mappings = {
  "/$controller/show/$id?"(parseRequest:false,action:'show'){
        constraints {
            // apply constraints here
        }
    }

  "/$controller/$action?/$id?"(parseRequest:true){
        constraints {
            // apply constraints here
        }
    }

(是的,这仍然发生在2.0.0 RC1中)

答案 1 :(得分:0)

你的控制器中是否有一个如下所示的动作:

def myaction.json()

如果没有,那么您将数据发送到不存在的操作。 如果您正在尝试解析JSON,那么请使用grails.converters:

import grails.converters

def jsonData = JSON.parse(params)

这个tuto也可能会有所帮助: http://www.ibm.com/developerworks/java/library/j-grails11188/index.html