springmvc.xml配置文件中扫描的包是package=“com.test.controller”,而testException在com.test.exception包下,无法扫描到这个类,所以该全局异常处理类未生效
解决方案: 将该类移到controller包下,即可扫描到,也就是说交给了ioc容器,开始生效
1.Controller层的代码
//web层 @RestController @RequestMapping("/checkitem") public class CheckItemController { @Reference //远程注入 com.alibaba.dubbo.config.annotation.Reference; private CheckItemService checkItemService; @RequestMapping("/test") public Result test(@RequestBody QueryPageBean queryPageBean) { PageResult pageResult = checkItemService.findPage(queryPageBean); int i = 1 / 0;//模拟异常 return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS); } }2.全局异常处理器
/** * 全局异常处理器, */ @ControllerAdvice public class testException { @ExceptionHandler(Exception.class) @ResponseBody public Result myException() { return new Result(false, "全局异常处理器", "this is a data"); } }3.浏览器返回的结果