@RestControllerAdvice注解作用

背景

全局接口异常处理,与@ExceptionHandler(value = Exception.class)一起使用可以全局捕获指定异常。

使用举例

@RestControllerAdvice
public class GlobalExceptionHandler {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
 
    @ExceptionHandler(Exception.class)
    public String exception(Exception e) {
        return "这是一个全局处理异常 "  + JSONObject.toJSONString(e);
    }
 
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public String arrayOutOfRangeException(Exception e) {
        LOGGER.error("数组越界 ",e);
        return "数组越界 "  + e.getMessage();
    }
 
}
————————————————
版权声明:本文为CSDN博主「not back」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xby7437/article/details/105974923

参考

posted @ 2022-02-17 11:35  zjcfrancis  阅读(110)  评论(0编辑  收藏  举报