不要再滿屏寫 try...catch 了!這個更香!

前言
推薦理由
代碼復(fù)制到項目中通過簡單的配置即可實現(xiàn)
可以靈活的根據(jù)自己的業(yè)務(wù)異常進行更細粒度的擴展
實踐
1 封裝統(tǒng)一返回結(jié)果類

public class AjaxResult {
//是否成功
private Boolean success;
//狀態(tài)碼
private Integer code;
//提示信息
private String msg;
//數(shù)據(jù)
private Object data;
public AjaxResult() {
}
//自定義返回結(jié)果的構(gòu)造方法
public AjaxResult(Boolean success,Integer code, String msg,Object data) {
this.success = success;
this.code = code;
this.msg = msg;
this.data = data;
}
//自定義異常返回的結(jié)果
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;
}
//其他異常處理方法返回的結(jié)果
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;
/**
* 錯誤狀態(tài)碼
*/
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 {
// 數(shù)據(jù)操作錯誤定義
SUCCESS(200, 成功),
NO_PERMISSION(403,你沒得權(quán)限),
NO_AUTH(401,未登錄),
NOT_FOUND(404, 未找到該資源!),
INTERNAL_SERVER_ERROR(500, 服務(wù)器異常請聯(lián)系管理員),
;
/** 錯誤碼 */
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 全局異常處理類

/**
* 全局異常處理器
*
*/
@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 測試


推薦閱讀: 一款Java工具庫,讓你的代碼量減少80% 一款基于 SpringBoot 的接口快速開發(fā)框架 據(jù)說Java程序員等電梯的時候,都想過調(diào)度算法 還在用分頁?太Low !試試 MyBatis 流式查詢! 最近面試BAT,整理一份面試資料《Java面試BAT通關(guān)手冊》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。 獲取方式:關(guān)注公眾號并回復(fù) java 領(lǐng)取,更多內(nèi)容陸續(xù)奉上。 明天見(??ω??)??
評論
圖片
表情
