- 前言
- 推荐理由
- 实践
-
- 封装统一返回结果类
-
- 自定义异常封装类
-
- 错误枚举,拒绝硬编码
-
- 全局异常处理类
-
- 测试
前言
软件开发 springboot 项目过程中,不可避免的需要处理各种异常,spring mvc 架构中各层会出现大量的 try {…} catch {…} finally {…} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。这样就需要定义个全局统一异常处理器,以便业务层再也不必处理异常。
推荐理由
- 代码复制到项目中通过简单的配置即可实现
- 可以灵活的根据自己的业务异常进行更细粒度的扩展
实践
1. 封装统一返回结果类
package com.az.bigdata.bi.web.activity.common;
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. 自定义异常封装类
package com.az.bigdata.bi.web.activity.common;
public class BusinessException extends RuntimeException {
public 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. 错误枚举,拒绝硬编码
package com.az.bigdata.bi.web.activity.common;
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. 错误枚举,拒绝硬编码
package com.az.bigdata.bi.web.activity.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
public static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = com.az.bigdata.bi.web.activity.common.BusinessException.class)
public AjaxResult bizExceptionHandler(com.az.bigdata.bi.web.activity.common.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. 测试
代码的书写
返回的结果: