自定義注解和springAOP捕獲Service層異常,并處理自定義異常操作
更新時間:2021年06月07日 11:27:39 作者:侯賽雷
這篇文章主要介紹了自定義注解和springAOP捕獲Service層異常,并處理自定義異常操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
一 自定義異常
/** * 自定義參數(shù)為null異常 */ public class NoParamsException extends Exception { //用詳細信息指定一個異常 public NoParamsException(String message){ super(message); } //用指定的詳細信息和原因構(gòu)造一個新的異常 public NoParamsException(String message, Throwable cause){ super(message,cause); } //用指定原因構(gòu)造一個新的異常 public NoParamsException(Throwable cause) { super(cause); } }
二 自定義注解
/** * 統(tǒng)一捕獲service異常處理注解 */ @Documented @Target({ElementType.METHOD, ElementType.TYPE}) //可在類或者方法使用 @Retention(RetentionPolicy.RUNTIME) public @interface ServiceExceptionCatch { }
三 注解切面處理類
@Component @Aspect @Slf4j public class ServiceExceptionHandler { @Around("@annotation(com.zhuzher.annotations.ServiceExcepCatch) || @within(com.zhuzher.annotations.ServiceExcepCatch)") public ResponseMessage serviceExceptionHandler(ProceedingJoinPoint proceedingJoinPoint) { ResponseMessage returnMsg; try { returnMsg = (ResponseMessage) proceedingJoinPoint.proceed(); } catch (Throwable throwable) { log.error("ServiceExcepHandler serviceExcepHandler failed", throwable); //單獨處理缺少參數(shù)異常 if(throwable instanceof NoParamsException) { returnMsg = ResponseMessage.failture(ErrorCode.ARG_CAN_NOT_BE_EMPTY); }else{//其他正常返回 returnMsg=ResponseMessage.newErrorsMessage(throwable.getMessage()); } } return returnMsg; } }
即可捕獲改異常,并自定義處理邏輯!
捕獲Service層異常,統(tǒng)一處理
新增注解,實現(xiàn)類和方法層級的異常捕獲
package com.ahdruid.aop.annotation; import java.lang.annotation.*; /** * 服務異常捕獲,如捕獲Service向外拋出的異常 * <p> * 添加在類上、方法上 * */ @Documented @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ServiceExcepCatch { }
異常處理handler
package com.ahdruid.aop; import com.ahdruid.ReturnMsg; import com.ahdruid.errorenums.BaseErrorEnum; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; /** * 服務異常捕獲處理器 * <p> * 如捕獲Service向外拋出的異常 * */ @Component @Aspect @Slf4j public class ServiceExcepHandler { @Around("@annotation(com.ahdruid.aop.annotation.ServiceExcepCatch) || @within(com.ahdruid.aop.annotation.ServiceExcepCatch)") public ReturnMsg serviceExcepHandler(ProceedingJoinPoint proceedingJoinPoint) { ReturnMsg returnMsg = new ReturnMsg(); try { returnMsg = (ReturnMsg) proceedingJoinPoint.proceed(); } catch (Throwable throwable) { log.error("ServiceExcepHandler serviceExcepHandler failed", throwable); returnMsg.setError(BaseErrorEnum.SYS_ERROR_UNKNOW); } return returnMsg; } }
使用時,在類或者方法上加上注解@ServiceExcepCatch
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java分布式鎖、分布式ID和分布式事務的實現(xiàn)方案
在分布式系統(tǒng)中,分布式鎖、分布式ID和分布式事務是常用的組件,用于解決并發(fā)控制、唯一標識和數(shù)據(jù)一致性的問題,本文將介紹Java中常用的分布式鎖、分布式ID和分布式事務的實現(xiàn)方案,并通過具體的示例代碼演示它們的用法和應用場景2023-06-06基于java+springboot+mybatis+laiyu實現(xiàn)學科競賽管理系統(tǒng)
這篇文章主要介紹了基于java+springboot+mybatis+laiyu實現(xiàn)的學科競賽管理系統(tǒng),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09在SpringBoot中實現(xiàn)一個訂單號生成系統(tǒng)的示例代碼
在Spring Boot中設(shè)計一個訂單號生成系統(tǒng),主要考慮到生成的訂單號需要滿足的幾個要求:唯一性、可擴展性、以及可能的業(yè)務相關(guān)性,本文給大家介紹了幾種常見的解決方案及相應的示例代碼,需要的朋友可以參考下2024-02-02@JsonFormat處理LocalDateTime失效的問題
這篇文章主要介紹了關(guān)于@JsonFormat處理LocalDateTime失效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08JAVA 16位ID生成工具類含16位不重復的隨機數(shù)數(shù)字+大小寫
這篇文章主要介紹了JAVA 16位ID生成工具類含16位不重復的隨機數(shù)數(shù)字+大小寫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02