在service-base中创建统一异常处理类GlobalExceptionHandler.java。
package com.wzy.servicebase.exception;import com.wzy.commonutils.R;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;//统一异常处理类@ControllerAdvicepublic class GlobalExceptionHandler {//全局异常,当出现 Exception 异常 ,返回 R.error() 错误提示@ExceptionHandler(Exception.class)@ResponseBodypublic R error(Exception e){e.printStackTrace();return R.error().message("全局异常处理!");}//特定异常,当出现 异常 ,先找是不是 ArithmeticException 的异常,不是就执行上面的异常@ExceptionHandler(ArithmeticException.class)@ResponseBodypublic R error(ArithmeticException e){e.printStackTrace();return R.error().message("ArithmeticException异常处理!");}}
注意:要使用 R 类需要引入 它所在模块的依赖。
- 在
service-base的 pom.xml 中引入 R 类 所在的 common-utils 模块的 父模块的依赖。<dependencies><dependency><groupId>com.wzy</groupId><artifactId>common-utils</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
