求求你不要写满屏的 try...catch 了,这才是优雅的处理方式,真香...

简介: 求求你不要写满屏的 try...catch 了,这才是优雅的处理方式,真香...

前言

软件开发springboot项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的try {...} catch {...} finally {...}代码块,不仅有大量的冗余代码,而且还影响代码的可读性。


Spring Boot 基础就不介绍了,推荐下这个实战教程: https://github.com/javastacks/spring-boot-best-practice

这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。


推荐理由

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


实践

1 封装统一返回结果类

image.png

源代码

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 自定义异常封装类

image.png

源码:

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 错误枚举,拒绝硬编码

image.png


源码

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 全局异常处理类

image.png

源码

/**
 * 全局异常处理器
 *
 */
@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 exceptionHandler( Exception e) {
      log.error(e.getMessage(), e);
        return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
    }
}

5 测试

image.png返回结果:

image.png






相关文章
|
JavaScript
JS设置日期为0时0分0秒
项目中经常要给设置默认值,搜索从哪天开始,这时候,如果直接通过new Date()来获取时间,会有时分秒,如果快速设置为0时0分0秒?
693 0
|
10月前
|
机器学习/深度学习 数据采集 传感器
基于极限学习机和BP神经网络的半监督分类算法
基于极限学习机(Extreme Learning Machine, ELM)和反向传播(Backpropagation, BP)神经网络的半监督分类算法,旨在结合两者的优势:​**ELM的快速训练能力**和**BP的梯度优化能力**,同时利用少量标注数据和大量未标注数据提升分类性能。
261 6
|
监控 前端开发 开发者
前端代码规范 - 日志打印规范
前端代码规范 - 日志打印规范
|
Ubuntu Linux 数据安全/隐私保护
Linux Ubuntu crontab 添加错误 提示:no crontab for root - using an empty one 888
Linux Ubuntu crontab 添加错误 提示:no crontab for root - using an empty one 888
741 3
|
存储 运维 前端开发
淘宝 NPM 镜像站切换新域名啦
用CNPM/淘宝源的开发者们请注意,淘宝NPM 镜像站喊你切换新域名啦。新的Web 站点:https://npmmirror.com,Registry Endpoint:https://registry.npmmirror.com。随着新的域名已经正式启用,老 http://npm.taobao.org 和 http://registry.npm.taobao.org 域名将于 2022 年 05 月 31 日零时起停止服务。(望周知,求转发)
16962 1
|
NoSQL 关系型数据库 网络安全
前后端分离项目Docker部署指南(上)
前后端分离项目Docker部署指南(上)
1694 1
|
测试技术 Linux Go
|
网络协议 安全 Linux
Deepin系统安装x11vnc远程桌面工具实现无公网ip访问本地桌面
Deepin系统安装x11vnc远程桌面工具实现无公网ip访问本地桌面
968 0
|
数据采集 Linux API
Python爬虫实践指南:利用cpr库爬取技巧
Python爬虫实践指南:利用cpr库爬取技巧

热门文章

最新文章