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

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

 前言

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

推荐理由

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

实践

1.封装统一返回结果类

 

  1. public class AjaxResult {  
  2.  //是否成功  
  3.     private Boolean success;  
  4.     //状态码  
  5.     private Integer code;  
  6.     //提示信息  
  7.     private String msg;  
  8.     //数据  
  9.     private Object data;  
  10.     public AjaxResult() {  
  11.     }  
  12.     //自定义返回结果的构造方法  
  13.     public AjaxResult(Boolean success,Integer code, String msg,Object data) {  
  14.         this.success = success;  
  15.         this.code = code;  
  16.         this.msg = msg;  
  17.         this.data = data;  
  18.     }  
  19.     //自定义异常返回的结果  
  20.     public static AjaxResult defineError(BusinessException de){  
  21.      AjaxResult result = new AjaxResult();  
  22.         result.setSuccess(false);  
  23.         result.setCode(de.getErrorCode());  
  24.         result.setMsg(de.getErrorMsg());  
  25.         result.setData(null);  
  26.         return result;  
  27.     }  
  28.     //其他异常处理方法返回的结果  
  29.     public static AjaxResult otherError(ErrorEnum errorEnum){  
  30.      AjaxResult result = new AjaxResult();  
  31.         result.setMsg(errorEnum.getErrorMsg());  
  32.         result.setCode(errorEnum.getErrorCode());  
  33.         result.setSuccess(false);  
  34.         result.setData(null);  
  35.         return result;  
  36.     }  
  37.  public Boolean getSuccess() {  
  38.   return success;  
  39.  }  
  40.  public void setSuccess(Boolean success) {  
  41.   this.success = success;  
  42.  }  
  43.  public Integer getCode() {  
  44.   return code;  
  45.  }  
  46.  public void setCode(Integer code) {  
  47.   this.code = code;  
  48.  }  
  49.  public String getMsg() {  
  50.   return msg;  
  51.  }  
  52.  public void setMsg(String msg) { 
  53.   this.msg = msg; 
  54.  }  
  55.  public Object getData() {  
  56.   return data;  
  57.  }  
  58.  public void setData(Object data) {  
  59.   this.data = data;  
  60.  }    

2 自定义异常封装类

 

  1. public class BusinessException extends RuntimeException {  
  2.  private static final long serialVersionUID = 1L 
  3.  /**  
  4.   * 错误状态码  
  5.   */  
  6.  protected Integer errorCode;  
  7.  /**  
  8.   * 错误提示  
  9.   */  
  10.  protected S  
  11.  public BusinessException(){  
  12.      }  
  13.  public BusinessException(Integer errorCode, String errorMsg) {  
  14.          this.errorCode = errorCode;  
  15.          this.errorMsg = errorMsg;  
  16.      }  
  17.  public Integer getErrorCode() { 
  18.   return errorCode;  
  19.  }  
  20.  public void setErrorCode(Integer errorCode) {  
  21.   this.errorCode = errorCode;  
  22.  }  
  23.  public String getErrorMsg() {  
  24.   return errorMsg;  
  25.  }  
  26.  public void setErrorMsg(String errorMsg) {  
  27.   this.errorMsg = errorMsg;  
  28.  }  

3 错误枚举,拒绝硬编码

 

  1. public enum ErrorEnum {  
  2.  // 数据操作错误定义  
  3.  SUCCESS(200, "成功"),  
  4.  NO_PERMISSION(403,"你没得权限"), 
  5.  NO_AUTH(401,"未登录"),  
  6.  NOT_FOUND(404, "未找到该资源!"),  
  7.  INTERNAL_SERVER_ERROR(500, "服务器异常请联系管理员"),  
  8.  ;  
  9.  /** 错误码 */  
  10.  private Integer errorCode;  
  11.  /** 错误信息 */  
  12.  private String errorMsg;  
  13.  ErrorEnum(Integer errorCode, String errorMsg) {  
  14.   this.errorCode = errorCode;  
  15.   this.errorMsg = errorMsg;  
  16.  }  
  17.     public Integer getErrorCode() {  
  18.         return errorCode;  
  19.     }  
  20.     public String getErrorMsg() {  
  21.         return errorMsg;  
  22.     }  

4 全局异常处理类

 

  1. /**  
  2.  * 全局异常处理器  
  3.  *   
  4.  */  
  5. @RestControllerAdvice  
  6. public class GlobalExceptionHandler  
  7.  
  8.     private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);  
  9.     /**  
  10.      * 处理自定义异常  
  11.      *  
  12.      */ 
  13.     @ExceptionHandler(value = BusinessException.class)  
  14.     public AjaxResult bizExceptionHandler(BusinessException e) {  
  15.      log.error(e.getMessage(), e);  
  16.         return AjaxResult.defineError(e);  
  17.     }  
  18.     /**  
  19.               *            处理其他异常  
  20.      *  
  21.      */ 
  22.     @ExceptionHandler(value = Exception.class) 
  23.     public AjaxResult exceptionHandler( Exception e) {  
  24.       log.error(e.getMessage(), e);  
  25.         return AjaxResult.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);    
  26.      } 

5 测试

返回结果:

 

 

责任编辑:庞桂玉 来源: java版web项目
相关推荐

2020-06-15 08:12:51

try catch代码处理器

2023-12-05 14:10:00

接口可读性

2020-05-26 13:48:05

后端框架异常

2020-10-12 10:45:44

nullava程序员

2019-08-22 14:02:00

Spring BootRestful APIJava

2020-05-09 10:18:31

Java开源工具

2018-04-27 14:18:01

2014-08-21 14:49:32

MIUI 6

2020-12-11 09:24:19

Elasticsear存储数据

2021-05-11 07:10:18

标准库DjangoOS

2021-05-17 14:57:23

策略模式代码

2023-11-13 17:01:26

C++编程

2011-04-20 11:04:23

LinuxHTTP 302

2020-12-15 08:06:45

waitnotifyCondition

2009-08-27 09:57:24

Power7处理器

2021-06-22 05:41:07

Windows10操作系统微软

2023-03-09 09:43:56

架构技术

2012-10-30 10:50:26

AMDARM架构处理器

2022-01-25 12:14:39

面试try-catch代码

2009-07-21 14:30:38

Scalatry-catch
点赞
收藏

51CTO技术栈公众号