作者:小李子说程序\
起源:https://www.toutiao.com/i6878...

前言

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

Spring Boot 根底就不介绍了,举荐下这个实战教程:
https://github.com/javastacks...

这样就须要定义个全局对立异样处理器,以便业务层再也不用解决异样。

举荐理由

  • 代码复制到我的项目中通过简略的配置即可实现
  • 能够灵便的依据本人的业务异样进行更细粒度的扩大

实际

1 封装对立返回后果类

源代码

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

源码:

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

源码

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 全局异样解决类

源码

/** * 全局异样处理器 * */@RestControllerAdvicepublic 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 测试

返回后果:

近期热文举荐:

1.1,000+ 道 Java面试题及答案整顿(2022最新版)

2.劲爆!Java 协程要来了。。。

3.Spring Boot 2.x 教程,太全了!

4.别再写满屏的爆爆爆炸类了,试试装璜器模式,这才是优雅的形式!!

5.《Java开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞+转发哦!