我在Grails 2.3.11控制器according to this proposal on SO上使用动态mixins,原因是:
class SomeApiController {
def SomeApiController() {
SomeApiController.mixin MyControllerMixin
}
...
不幸的是,它不适用于异常处理程序,例如:
class MyControllerMixin {
...
def businessException( BusinessException e ) {
log.error( "API exception: ${e.message} ${e.errorCode}", e )
def result = [
status: 'Failure',
errorCode: e.errorCode.name()
]
response.status = 400
render result as JSON
}
...
}
在这种情况下,SomeApiController
中的默认异常处理程序在从某处抛出BusinessException
时被调用。如果我将处理程序直接放在控制器中,它可以很好地工作,但这是我想要避免的。
是否有任何解决方法可以使其正常工作?
答案 0 :(得分:1)
见http://grails.org/doc/latest/guide/theWebLayer.html#controllerExceptionHandling。这包括以下内容......
编译时必须存在异常处理程序方法。 特别是,运行时的异常处理程序方法 不支持元编程到控制器类上。