自定义注解和springAOP捕获Service层异常,并处理自定义异常操作
作者:侯赛雷 时间:2023-04-04 05:26:04
一 自定义异常
/**
* 自定义参数为null异常
*/
public class NoParamsException extends Exception {
//用详细信息指定一个异常
public NoParamsException(String message){
super(message);
}
//用指定的详细信息和原因构造一个新的异常
public NoParamsException(String message, Throwable cause){
super(message,cause);
}
//用指定原因构造一个新的异常
public NoParamsException(Throwable cause) {
super(cause);
}
}
二 自定义注解
/**
* 统一捕获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);
//单独处理缺少参数异常
if(throwable instanceof NoParamsException) {
returnMsg = ResponseMessage.failture(ErrorCode.ARG_CAN_NOT_BE_EMPTY);
}else{//其他正常返回
returnMsg=ResponseMessage.newErrorsMessage(throwable.getMessage());
}
}
return returnMsg;
}
}
即可捕获改异常,并自定义处理逻辑!
捕获Service层异常,统一处理
新增注解,实现类和方法层级的异常捕获
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
来源:https://www.cnblogs.com/houzheng/p/11953183.html
标签:注解,springAOPService,异常
0
投稿
猜你喜欢
SpringBoot Security密码加盐实例
2023-06-08 17:06:48
IDEA报错:无效的源发行版解决方案
2022-06-05 08:38:58
c#读写excel文件使用示例
2021-08-27 03:02:53
Mybatis RowBounds 限制查询条数的实现代码
2022-11-18 17:26:03
C#在Windows窗体控件实现内容拖放(DragDrop)功能
2021-07-25 01:48:27
java动态代理(jdk与cglib)详细解析
2022-06-01 19:14:21
Java中List与Map初始化的一些写法分享
2021-11-23 13:13:46
秒懂Kotlin之Java工程师快速掌握Kotlin的技巧
2023-07-09 21:25:19
Java基础之多线程
2022-11-30 11:01:43
Unity Shader实现模糊效果
2021-07-22 02:42:57
Java之OutputStreamWriter流案例详解
2023-11-11 13:03:55
解决Mybatis中foreach嵌套使用if标签对象取值的问题
2023-11-23 06:02:02
C#将制定目录文件名转换成大写的方法
2022-10-03 19:46:36
Android仿微信单击拍照长按录像功能实例代码
2022-01-16 17:45:07
JAVA实现基于Tcp协议的简单Socket通信实例
2022-07-07 21:44:36
5分钟快速学会spring boot整合JdbcTemplate的方法
2022-04-01 15:56:11
Java使用BigDecimal进行运算封装的实际案例
2023-06-20 02:22:26
MyBatis传入参数为List对象的实现
2021-12-14 20:16:06
@CacheEvict 清除多个key的实现方式
2023-11-21 08:28:04
Android中 动态改变对话框值的方法
2023-08-17 19:44:30