当我使用 .json 文件扩展名(例如http://localhost:8080/myapp/mycontroller/myaction.json )向我的 grails 1.3.7 控制器发送一个带有空正文的 GET 请求时,我得到一个请求解析异常,它似乎 grails 正试图将我的空体解析为 JSON。如果我向相同的操作发送相同的请求但没有 .json 扩展名,我没有任何错误。
我怎样才能摆脱这个错误?
当我使用 .json 文件扩展名(例如http://localhost:8080/myapp/mycontroller/myaction.json )向我的 grails 1.3.7 控制器发送一个带有空正文的 GET 请求时,我得到一个请求解析异常,它似乎 grails 正试图将我的空体解析为 JSON。如果我向相同的操作发送相同的请求但没有 .json 扩展名,我没有任何错误。
我怎样才能摆脱这个错误?
我最好的办法是在 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 中)
您的控制器中是否有如下所示的操作:
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