控制器中的Grails错误处理

时间:2016-10-18 19:24:46

标签: grails

我想呈现自定义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?

1 个答案:

答案 0 :(得分:2)

我认为你想要的是request.forwardURI。这应该会给你一个触发错误的请求。