经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
自定义注解和springAOP捕获Service层异常,并处理自定义异常操作
来源:jb51  时间:2021/6/7 13:21:59  对本文有异议

一 自定义异常

  1. /**
  2. * 自定义参数为null异常
  3. */
  4. public class NoParamsException extends Exception {
  5. //用详细信息指定一个异常
  6. public NoParamsException(String message){
  7. super(message);
  8. }
  9.  
  10. //用指定的详细信息和原因构造一个新的异常
  11. public NoParamsException(String message, Throwable cause){
  12. super(message,cause);
  13. }
  14.  
  15. //用指定原因构造一个新的异常
  16. public NoParamsException(Throwable cause) {
  17. super(cause);
  18. }
  19. }

二 自定义注解

  1. /**
  2. * 统一捕获service异常处理注解
  3. */
  4. @Documented
  5. @Target({ElementType.METHOD, ElementType.TYPE}) //可在类或者方法使用
  6. @Retention(RetentionPolicy.RUNTIME)
  7. public @interface ServiceExceptionCatch {
  8. }

三 注解切面处理类

  1. @Component
  2. @Aspect
  3. @Slf4j
  4. public class ServiceExceptionHandler {
  5.  
  6. @Around("@annotation(com.zhuzher.annotations.ServiceExcepCatch) || @within(com.zhuzher.annotations.ServiceExcepCatch)")
  7. public ResponseMessage serviceExceptionHandler(ProceedingJoinPoint proceedingJoinPoint) {
  8. ResponseMessage returnMsg;
  9. try {
  10. returnMsg = (ResponseMessage) proceedingJoinPoint.proceed();
  11. } catch (Throwable throwable) {
  12. log.error("ServiceExcepHandler serviceExcepHandler failed", throwable);
  13. //单独处理缺少参数异常
  14. if(throwable instanceof NoParamsException) {
  15. returnMsg = ResponseMessage.failture(ErrorCode.ARG_CAN_NOT_BE_EMPTY);
  16. }else{//其他正常返回
  17. returnMsg=ResponseMessage.newErrorsMessage(throwable.getMessage());
  18. }
  19. }
  20. return returnMsg;
  21. }
  22. }

即可捕获改异常,并自定义处理逻辑!

捕获Service层异常,统一处理

新增注解,实现类和方法层级的异常捕获

  1. package com.ahdruid.aop.annotation;
  2. import java.lang.annotation.*;
  3. /**
  4. * 服务异常捕获,如捕获Service向外抛出的异常
  5. * <p>
  6. * 添加在类上、方法上
  7. *
  8. */
  9. @Documented
  10. @Target({ElementType.METHOD, ElementType.TYPE})
  11. @Retention(RetentionPolicy.RUNTIME)
  12. public @interface ServiceExcepCatch {
  13. }

异常处理handler

  1. package com.ahdruid.aop;
  2. import com.ahdruid.ReturnMsg;
  3. import com.ahdruid.errorenums.BaseErrorEnum;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. * 服务异常捕获处理器
  11. * <p>
  12. * 如捕获Service向外抛出的异常
  13. *
  14. */
  15. @Component
  16. @Aspect
  17. @Slf4j
  18. public class ServiceExcepHandler {
  19. @Around("@annotation(com.ahdruid.aop.annotation.ServiceExcepCatch) || @within(com.ahdruid.aop.annotation.ServiceExcepCatch)")
  20. public ReturnMsg serviceExcepHandler(ProceedingJoinPoint proceedingJoinPoint) {
  21. ReturnMsg returnMsg = new ReturnMsg();
  22. try {
  23. returnMsg = (ReturnMsg) proceedingJoinPoint.proceed();
  24. } catch (Throwable throwable) {
  25. log.error("ServiceExcepHandler serviceExcepHandler failed", throwable);
  26. returnMsg.setError(BaseErrorEnum.SYS_ERROR_UNKNOW);
  27. }
  28. return returnMsg;
  29. }
  30. }

使用时,在类或者方法上加上注解@ServiceExcepCatch

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号