Grails 全局异常捕获

package middol.global

import grails.converters.JSON

/**
* @Description:    全局异常处理
* @Author:         [email protected]
* @CreateDate:     2020/3/19 14:57
* @Version:        1.0
*/
trait GlobalExceptionHandle {

    Result handle(Exception e){
        render ResultUtils.error(e.getMessage()) as JSON
    }
}

然后在controller里面去实现这个接口就可以了

package middol.test


import grails.rest.*
import grails.converters.*
import middol.global.BusinessException
import middol.global.GlobalExceptionHandle

class TestController implements GlobalExceptionHandle{
	static responseFormats = ['json', 'xml']
	
    def index() throws Exception{
        if(true){
            throw new BusinessException("出异常啦")
        }
    }
}

参考

发布了102 篇原创文章 · 获赞 40 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_16165281/article/details/104967889
今日推荐