```
/**
* @author 張躍帥
* @Description: 全局異常處理器
* @date 2020/08/12
*/
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalException {
/**
* 捕獲-自定義異常
*/
@ExceptionHandler(MyException.class)
public MyResult myException(MyException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
log.error(">>> 自定義異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(e.getCode(), e.getMsg());
}
/**
* 請求參數缺失異常
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public MyResult reqParamException(MissingServletRequestParameterException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
// 獲取參數
String parameterType = e.getParameterType();
String parameterName = e.getParameterName();
String msg = StrUtil.format("請求參數異常:請求的參數{},與接收{}的類型不匹配", parameterName, parameterType);
log.error(">>> 請求參數異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), msg);
}
/**
* 攔截參數格式傳遞異常
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public MyResult paramFormatException(HttpMessageNotReadableException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
log.error(">>> 參數格式傳遞異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "請求JSON參數格式不正確,請檢查參數格式");
}
/**
* 捕獲-字段值,超出數據庫字段的長度
*/
@ExceptionHandler(DataIntegrityViolationException.class)
public MyResult fieldValLengthException(DataIntegrityViolationException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
log.error(">>> 字段值異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "當前字段值:超出數據庫字段的存儲長度");
}
/**
* 主鍵或unique索引,數據重復異常
*/
@ExceptionHandler(DuplicateKeyException.class)
public MyResult duplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
log.error(">>> 主鍵或unique索引重復異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "主鍵或unique索引重復異常:" + e.getMessage());
}
/**
* Mybatis系統異常
*/
@ExceptionHandler(MyBatisSystemException.class)
public MyResult myBatisException(MyBatisSystemException e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
log.error(">>> Mybatis系統異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 獲取異常信息
String msg = e.getMessage();
// 判斷-是否包含
if (msg.contains("CannotFindDataSourceException")) {
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "mybatis未找到數據源,請聯系管理員");
}
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "mybatis異常:" + msg);
}
/**
* 攔截請求方式的異常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public MyResult reqTypeException(HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
// 參數-變量
String msg = null;
// 判斷-GET請求
if (ServletUtil.isGetMethod(request)) {
msg = "不支持該請求方法,請求方法為GET";
}
// 判斷-POST請求
if (ServletUtil.isPostMethod(request)) {
msg = "不支持該請求方法,請求方法為POST";
}
log.error(">>> 請求方式異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, msg);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), msg);
}
/**
* 文件上傳的異常
*/
@ExceptionHandler(MultipartException.class)
public MyResult uploadFileException(Exception e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
// 參數-變量
String msg = null;
// 判斷
if (e.getCause().getCause() instanceof FileSizeLimitExceededException) {
msg = "文件上傳過大[單文件大小不得超過500M]";
} else if (e.getCause().getCause() instanceof SizeLimitExceededException) {
msg = "文件上傳過大[總上傳文件大小不得超過1G]";
} else {
msg = "文件上傳失敗";
}
log.error(">>> 文件上傳異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, msg);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), msg);
}
/**
* 攔截未知的運行時異常
*/
@ExceptionHandler(Throwable.class)
public MyResult serverException(Throwable e, HttpServletRequest request) {
// 獲取參數
String userFullName = this.getReqUserFullName(request);
String currentReqURL = request.getRequestURI();
// 獲取異常信息
String msg = e.getMessage();
// 判斷-是否包含
if (msg.contains("SQLSyntaxErrorException")) {
log.error(">>> SQL語句錯誤異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_400.getCode(), "SQL語句錯誤異常:" + msg);
} else {
log.error(">>> 服務器運行異常→ 請求用戶:{},請求URL為:{},具體信息為:{}", userFullName, currentReqURL, e);
}
// 返回
return MyResult.error(CommonExceptionEnum.ERROR_500.getCode(), "服務器運行異常:" + msg);
}
/***
* 獲取請求用戶全稱
*/
private String getReqUserFullName (HttpServletRequest request) {
// 變量
String userFullName = null;
// 變量-是否放行請求URL
Boolean doReqURL = false;
// 獲取請求路徑
String servletPath = request.getServletPath();
// 獲取不攔截的URL
List<String> satokenFreeList = SatokenFreeRedisCache.getSatokenFreeList();
// 判斷
if (satokenFreeList != null && satokenFreeList.size() > 0) {
// 創建路徑匹配器
final PathMatcher pathMatcher = new AntPathMatcher();
// 遍歷
for (String satokenFree : satokenFreeList) {
// 判斷
if (pathMatcher.match(satokenFree, servletPath)) {
// 放行
doReqURL = true;
// 如果URL匹配成功就不循環了,直接退出循環
break;
}
}
}
// 判斷
if (false == doReqURL) {
// 獲取當前登錄用戶
SatokenUser satokenUser = LoginUserUtil.getLoginSatokenUserByToken();
// 獲取參數
String userName = satokenUser.getTrueName();
Long userId = satokenUser.getUserId();
// 賦值
userFullName = (userName + "-" + userId);
}
// 返回
return userFullName;
}
}
- Jump簡介
- 技術架構
- 代碼規范
- 規范導讀
- JAVA規范
- 數據庫表設計規范
- 集成項目
- JDK1.8-pom.xml
- JDK21-pom.xml
- 項目結構
- 業務模塊-方法名稱規范
- 跨域配置
- License授權配置
- 公共字段自動填充
- 全局異常處理器
- PageOffice配置
- Beetl模板引擎配置
- application.properties
- application-prod.yml
- banner.txt
- logback-spring.xml
- jump-core (核心組件)
- Maven依賴
- 通用枚舉
- 公共數據狀態 - 枚舉
- 公共邏輯刪除 - 枚舉
- 公共操作編碼類型 - 枚舉
- 公共tree父節點 - 枚舉
- 公共是或否 - 枚舉
- 工具Util
- AopTargetUtil
- DownloadUtil
- GenerateNumUtil
- HttpServletUtil
- IpUtil
- JoinPointUtil
- MacUtil
- NetworkUtil
- ParamToUtil
- ResponseUtil
- TimeZoneDateUtil
- UaUtil
- 統一返回
- 結果對象
- 如何使用
- jump-cahche (緩存組件)
- Maven依賴
- Redis配置
- 緩存常量
- 工具Util
- RedisCacheUtil
- jump-idempotent (幕等組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- AOP參數
- AOP使用方法
- jump-lock (分布式鎖組件)
- Maven依賴
- 枚舉
- AOP參數
- AOP使用方法
- 工具Util
- RedissonLockUtil
- Util使用方法
- jump-mybatis (mybatis組件)
- Maven依賴
- 基礎Entity
- 枚舉
- 查詢類型 - 枚舉
- 條件查詢
- search
- service
- 分頁結果集
- Mapper
- MyMapper
- 使用方法
- DDL操作
- DML操作
- 工具Util
- EntityUtil
- PageUtil
- TableUtil
- jump-dynamic-datasource (多數據源組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- 工具Util
- DatasourceUtil
- 如何使用
- jump-satoken (satoken組件)
- Maven依賴
- Context上下文
- 操作器
- 接口
- 如何實現
- Satoken配置信息
- SatokenUser信息
- Redis緩存操作
- SatokenRedisCache
- SatokenUserRedisCache
- 放行白名單
- jump-oss (OSS組件)
- Maven依賴
- 工具Util
- OssFileUtil
- OssPlatformUtil
- 如何使用
- jump-xss (XSS組件)
- Maven依賴
- 白名單放行
- jump-email (郵件組件)
- Maven依賴
- Email客戶端信息
- Email發送參數
- 工具Util
- jump-websocket (WebSocket組件)
- Maven依賴
- 消息對象
- 工具Util
- 如何使用
- jump-weixin (微信組件)
- Maven依賴
- jump-system (系統管理組件)
- Maven依賴
- AOP
- 系統操作日志AOP
- 系統數據日志AOP
- 系統操作權限AOP
- 字典轉文本AOP
- Redis緩存操作
- SystemConfigRedisCache
- 工具Util
- LoginUserUtil
- SystemAreaUtil
- SystemHomeUtil
- SystemMenuUtil
- SystemOrgAdminUtil
- SystemOrgTypeUtil
- SystemRoleUtil
- SystemUserLoginAreaUtil
- SystemUserUtil
- jump-timer(定時器組件)
- Maven依賴
- 枚舉
- Api接口
- 工具Util
- ActionClassUtil
- TimerTaskUtil
- 如何使用
- jump-ueditor (富文本組件)
- Maven依賴
- 如何使用
- 配置 ueditor.config.js
- 后端 application.properties
- 前端 vue3
- vue-codemirror (代碼編譯器)
- npm依賴
- PageOffice整合
- Maven依賴
- License授權配置
- 枚舉
- 文件來源 - 枚舉
- 預覽文件類型 - 枚舉
- 文件預覽參數
- 下載文件
- 預覽文件
- 工具Util