- 首先嫉沽,利用枚舉昔逗,來(lái)定義異常類型。定義枚舉ResultEnum:
package org.sqlb.enums;
/**
* 利用枚舉來(lái) 指定錯(cuò)誤碼
*/
public enum ResultEnum {
SUCCESS_RESULT(1,"成功"),
ERROR_RESULT(2,"參數(shù)錯(cuò)誤"),
;
private Integer status;
private String message;
ResultEnum(Integer status, String message) {
this.status = status;
this.message = message;
}
public Integer getStatus() {
return status;
}
public String getMessage() {
return message;
}
}
- 自定義異常至非,新建CustomException類:
package org.sqlb.exception;
import org.sqlb.enums.ResultEnum;
/**
* 自定義異常類钠署,添加一個(gè)status字段
* 這里只能繼承 RuntimeException 類,因?yàn)榛耐郑瑂pring只對(duì)該異常做了事物回滾
*/
public class CustomException extends RuntimeException {
private Integer status;
public CustomException(ResultEnum resultEnum) {
super(resultEnum.getMessage());
this.status = resultEnum.getStatus();
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
- 統(tǒng)一處理異常谐鼎,定義ExceptionHandle類。之所以統(tǒng)一定義異常處理類趣惠,是因?yàn)槔旯鳎覀兛梢酝ㄟ^(guò)catch異常,來(lái)給前端提供一個(gè)規(guī)范的數(shù)據(jù)返回引導(dǎo)味悄,便于前端對(duì)異常進(jìn)行處理草戈,從而給用戶一個(gè)較好的體驗(yàn)。
package org.sqlb.handle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.sqlb.bean.Result;
import org.sqlb.exception.CustomException;
import org.sqlb.utils.ResultUtil;
/**
* 統(tǒng)一異常處理
*/
@ControllerAdvice
public class ExceptionHandle {
private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result handle(Exception e) {
//在異常里面進(jìn)行返回?cái)?shù)據(jù)的處理
if (e instanceof CustomException) {
CustomException ex = (CustomException) e;
return ResultUtil.error(ex.getStatus(), e.getMessage());
}
logger.info("[系統(tǒng)異常]" ,e);
return ResultUtil.error(-1, "位置錯(cuò)誤");
}
}
附錄:
Result類:
package org.sqlb.bean;
/**
* 請(qǐng)求返回結(jié)果 統(tǒng)一結(jié)構(gòu) 類
*/
public class Result<T> {
private Integer status;
private String message;
private T result;
public Result() {
}
public Result(Integer status, String message, T result) {
this.status = status;
this.message = message;
this.result = result;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
}
ResultUtil類:
package org.sqlb.utils;
import org.sqlb.bean.Result;
/**
*統(tǒng)一處理response結(jié)果
*/
public class ResultUtil {
public static Result success(Object obj) {
Result result = new Result();
result.setResult(obj);
result.setStatus(1);
result.setMessage("成功");
return result;
}
public static Result success() {
return success(null);
}
public static Result error(Integer status,String message) {
Result result = new Result();
result.setStatus(status);
result.setMessage(message);
return result;
}
}