import com.xjzd.annotation.Log; import com.xjzd.base.entity.ResultBean; import com.xjzd.constant.ResultEnum; import com.xjzd.util.GsonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestControllerAdvice; @RestControllerAdvice public class GlobalExceptionHandler { public static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); * 接口 业务异常 * @ResponseBody @ExceptionHandler(ServiceException.class) @Log("接口异常了") public String businessInterfaceException(ServiceException e) { log.debug("返回错误:msg={}, e={}", e.getMessage(),e); ResultBean error = ResultBean.error(ResultEnum.ERROR_INFAC); return GsonUtils.objToJson(error); } * 拦截所有运行时的全局异常 @ExceptionHandler(RuntimeException.class) @ResponseBody @Log("系统运行异常") public String runtimeException(RuntimeException e) { log.debug("返回错误:msg={}, e={}", e.getMessage(),e); // 返回 JOSN ResultBean error = ResultBean.error(ResultEnum.ERROR); return GsonUtils.objToJson(error); } * 系统异常捕获处理 * @ExceptionHandler(Exception.class) @ResponseBody @Log("系统出错了") public String exception(Exception e) { log.debug("返回错误:msg={}, e={}", e.getMessage(),e); // 返回 JOSN ResultBean error = ResultBean.error(ResultEnum.ERROR_SYS); return GsonUtils.objToJson(error); } }