公司这套架构统一处理 try...catch 这么香,求求你不要再满屏写了,再发现扣绩效!

编程帮

共 7530字,需浏览 16分钟

 ·

2021-03-19 11:15


点击上方 蓝字 关注我们!



Java,Python,C/C++,Linux,PHP,Go,C#,QT,大数据,算法,软件教程,前端,简历,毕业设计等分类,资源在不断更新中... 点击领取

每天 11 点更新文章,饿了点外卖,点击 👉《无门槛外卖优惠券,每天免费领!》

来源:toutiao.com/i6878184496945070604/

  • 前言
  • 推荐理由
  • 实践
    • 1 封装统一返回结果类
    • 2 自定义异常封装类
    • 3 错误枚举,拒绝硬编码
    • 4 全局异常处理类
    • 5 测试

前言

软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。

推荐理由

  • 代码复制到项目中通过简单的配置即可实现
  • 可以灵活的根据自己的业务异常进行更细粒度的扩展

实践

1 封装统一返回结果类

开发好物推荐5之统一异常处理从此抛弃try与catch

源代码

public class AjaxResult {
 //是否成功
    private Boolean success;
    //状态码
    private Integer code;
    //提示信息
    private String msg;
    //数据
    private Object data;
    public AjaxResult() {

    }
    //自定义返回结果的构造方法
    public AjaxResult(Boolean success,Integer code, String msg,Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    //自定义异常返回的结果
    public static AjaxResult defineError(BusinessException de){
     AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(de.getErrorCode());
        result.setMsg(de.getErrorMsg());
        result.setData(null);
        return result;
    }
    //其他异常处理方法返回的结果
    public static AjaxResult otherError(ErrorEnum errorEnum){
     AjaxResult result = new AjaxResult();
        result.setMsg(errorEnum.getErrorMsg());
        result.setCode(errorEnum.getErrorCode());
        result.setSuccess(false);
        result.setData(null);
        return result;
    }
 public Boolean getSuccess() {
  return success;
 }
 public void setSuccess(Boolean success) {
  this.success = success;
 }
 public Integer getCode() {
  return code;
 }
 public void setCode(Integer code) {
  this.code = code;
 }
 public String getMsg() {
  return msg;
 }
 public void setMsg(String msg) {
  this.msg = msg;
 }
 public Object getData() {
  return data;
 }
 public void setData(Object data) {
  this.data = data;
 }

}

2 自定义异常封装类

开发好物推荐5之统一异常处理从此抛弃try与catch

源码:

public class BusinessException extends RuntimeException {
 private static final long serialVersionUID = 1L;
 /**
  * 错误状态码
  */

 protected Integer errorCode;
 /**
  * 错误提示
  */

 protected String errorMsg;

 public BusinessException(){

     }

 public BusinessException(Integer errorCode, String errorMsg) {
         this.errorCode = errorCode;
         this.errorMsg = errorMsg;
     }

 public Integer getErrorCode() {
  return errorCode;
 }

 public void setErrorCode(Integer errorCode) {
  this.errorCode = errorCode;
 }

 public String getErrorMsg() {
  return errorMsg;
 }

 public void setErrorMsg(String errorMsg) {
  this.errorMsg = errorMsg;
 }
}

3 错误枚举,拒绝硬编码

开发好物推荐5之统一异常处理从此抛弃try与catch

源码

public enum ErrorEnum {
 // 数据操作错误定义
 SUCCESS(200"成功"),
 NO_PERMISSION(403,"你没得权限"),
 NO_AUTH(401,"未登录"),
 NOT_FOUND(404"未找到该资源!"),
 INTERNAL_SERVER_ERROR(500"服务器异常请联系管理员"),
 ;

 /** 错误码 */
 private Integer errorCode;

 /** 错误信息 */
 private String errorMsg;

 ErrorEnum(Integer errorCode, String errorMsg) {
  this.errorCode = errorCode;
  this.errorMsg = errorMsg;
 }

    public Integer getErrorCode() {
        return errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }
}

4 全局异常处理类

开发好物推荐5之统一异常处理从此抛弃try与catch

源码

/**
 * 全局异常处理器
 *
 */

@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);



    /**
     * 处理自定义异常
     */

    @ExceptionHandler(value = BusinessException.class)
    public AjaxResult bizExceptionHandler(BusinessException e
{
     log.error(e.getMessage(), e);
        return AjaxResult.defineError(e);
    }

    /**
     * 处理其他异常
     */

    @ExceptionHandler(value = Exception.class)
    public AjaxResult exceptionHandlerException e
{
      log.error(e.getMessage(), e);
        return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);

    }

}

5 测试

开发好物推荐5之统一异常处理从此抛弃try与catch

返回结果:

开发好物推荐5之统一异常处理从此抛弃try与catch

大家好,我是艿艿~~~一个每天肝到 2 点钟的小胖子

最近在和朋友一起肝一个 SpringBoot2.4.2 + Vue 的开源项目:

https://github.com/YunaiV/ruoyi-vue-pro

记得 Star 关注下噢,胖友们的支持,真的很重要!

往期推荐

华为 Java 编程军规 !

干饭时间到,补贴大战再起!

IDEA 热部署神器 JRebel ,提升开发效率10倍!

API接口的安全设计验证:ticket,签名,时间戳

看完文章,饿了点外卖,点击 👉《无门槛外卖优惠券,每天免费领!》

END



若觉得文章对你有帮助,随手转发分享,也是我们继续更新的动力。


长按二维码,扫扫关注哦

「C语言中文网」官方公众号,关注手机阅读教程 


必备编程学习资料


目前收集的资料包括: Java,Python,C/C++,Linux,PHP,go,C#,QT,git/svn,人工智能,大数据,单片机,算法,小程序,易语言,安卓,ios,PPT,软件教程,前端,软件测试,简历,毕业设计,公开课 等分类,资源在不断更新中...


点击“阅读原文”,立即免费领取最新资料!
👇👇👇
https://mp.weixin.qq.com/s/2WFbD0y6ct13gnR8m0xiaQ


浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报