我想呈现自定义JSON错误消息。我创建了以下控制器:
class ErrorController {
static responseFormats = ['json']
def notAuthorized() {
render([error: "You are not authorized to access ${request.getRequestURL()} on this server.",
path: request.getRequestURI()] as JSON)
}
}
在UrlMappings
课程内,我有以下内容:
"401"(controller: 'error', action: 'notAuthorized')
一切正常,除了我GET http://localhost/path
以下错误消息:
{"error":"You are not authorized to access http://localhost:8080/error on this server.",
"path":"/error"}
因此request
对象中的URI / URL不指向请求给出的URI / URL。有没有办法获得正确的URI / URL?
答案 0 :(得分:2)
我认为你想要的是request.forwardURI
。这应该会给你一个触发错误的请求。