springboot将异常统一使用类似@Controller注解方式统一处理,而不是传统的在每一个controller中处理
处理系统全局异常

使用@ControllerAdvice配合@ExceptionHandler注解使用
当java代码抛出系统异常时,@ControllerAdvice注解便回拦截,及逆行服务,@ExceptionHandler注解是指定处理哪一类的异常,类似Controller,可以返回json数据,也可以返回错误页面
处理用户自定义异常

用户自定义异常类定义

处理用户自定义异常的controller

测试自定义异常
网友评论