在我们的应用程序中,我们将一组 HTTP 错误代码映射到一个错误控制器,该错误控制器生成一个带有相应 http 错误代码作为响应状态的 json 消息。URLMappings.groovy 的相关部分非常简单:
"501"(controller: "error", action: "index")
"500"(controller: "error", action: "index")
"422"(controller: "error", action: "index")
"415"(controller: "error", action: "index")
"406"(controller: "error", action: "index")
"405"(controller: "error", action: "index")
"404"(controller: "error", action: "index")
"403"(controller: "error", action: "index")
"401"(controller: "error", action: "index")
"400"(controller: "error", action: "index")
现在,我正在尝试将 ErrorController.groovy 类和上面的 url 映射从应用程序中取出,并将其作为插件,以便我们可以在其他应用程序中进行相同的错误处理。
在我的名为“error-controller”的插件中,我创建了一个 ErrorControllerUrlMappings.groovy 类,并将上述映射移至此处。在这个 UrlMappings.groovy 中,似乎我可以定义任何我想要的映射,例如“/foo”、“/bar”等……但我无法获取要注册的 HTTP 错误代码映射。
当我从安装插件的应用程序触发这些错误代码之一时,我会收到该错误代码的默认 tomcat 消息。插件中的 ErrorController 永远不会被命中。
有趣的是,插件中的控制器正在处理 500 个错误。
是否可以在我的插件的 ErrorControllerUrlMappings.groovy 中为这些 http 错误代码创建 URL 映射?
- 谢谢