GlobalException:
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalException {
/**
* 系統異常處理
*
* @param ex 異常
* @return
*/
@ExceptionHandler(value = Throwable.class)
public Result defaultErrorHandler(Exception ex) {
log.error("系統異常:{}", ex);
return Result.error(StatusEnum.SYSTEM_ERROR);
}
/**
* BindException
*
* @param e 異常
* @return
*/
@ExceptionHandler(BindException.class)
public Result handleBingException(BindException e) {
FieldError fieldError = e.getFieldError();
String message = fieldError.getDefaultMessage();
return Result.error(StatusEnum.SYSTEM_ERROR);
}
/**
* 處理 @RequestBody參數校驗異常
*
* @param e 異常信息
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
FieldError fieldError = bindingResult.getFieldError();
String code = fieldError.getCode();
String message = fieldError.getDefaultMessage();
return Result.error(errorCode, message);
}
}
result:
@Data
public class Result<T> {
/**
* 狀態值
*/
private Boolean status;
/**
* 狀態碼
*/
private String code;
/**
* 狀態信息
*/
private String message;
/**
* 返回的結果
*/
private T result;
/**
* 構造函數
*
* @param code 狀態碼
* @param message 提示信息
*/
public Result(String code, String message) {
this.status = false;
this.code = code;
this.message = message;
}
/**
* 構造函數
*
* @param status 狀態
* @param code 狀態碼
* @param message 提示信息
*/
public Result(Boolean status, String code, String message) {
this.status = status;
this.code = code;
this.message = message;
}
/**
* 構造函數
*
* @param status 狀態
* @param code 狀態碼
* @param message 提示信息
* @param result 結果
*/
public Result(Boolean status, String code, String message, T result) {
this.status = status;
this.code = code;
this.message = message;
this.result = result;
}
// --------- 靜態方法 ----------
/**
* 枚舉的狀態碼
*/
private static final String CODE;
/**
* 枚舉的提示信息
*/
private static final String MESSAGE;
/**
* 500
*/
private static final String ERROR_CODE;
/**
* 成功返回的狀態碼
*/
private static final String SUCCESS_CODE;
/**
* 成功返回的狀態信息
*/
private static final String SUCCESS_MSG;
/**
* 靜態代碼塊初始化
*/
static {
// 成功返回的狀態碼
CODE = "getCode";
// 枚舉的提示信息
MESSAGE = "getMessage";
// 異常碼
ERROR_CODE = "500";
// 成功返回的狀態碼
SUCCESS_CODE = "200";
// 成功返回的狀態信息
SUCCESS_MSG = "操作成功";
}
/**
* 操作成功
*
* @return 返回結果
*/
public static Result success() {
return new Result(true, SUCCESS_CODE, SUCCESS_MSG);
}
/**
* 操作成功
*
* @return 返回結果
*/
public static <T> Result success(T result) {
return new Result(true, SUCCESS_CODE, SUCCESS_MSG, result);
}
/**
* 返回異常的
*
* @param t 泛型枚舉
* @param <T> 泛型
* @return 返回 result
*/
public static <T extends Enum> Result error(T t) {
return error(t, CODE, MESSAGE, null);
}
/**
* 返回異常的
*
* @param t 泛型枚舉
* @param message 提示信息
* @param <T> 泛型
* @return 返回 result
*/
public static <T extends Enum> Result error(T t, String message) {
return error(t, CODE, MESSAGE, message);
}
/**
* 返回異常的
*
* @param code 狀態碼
* @return 返回 result
*/
public static Result error(String code) {
return new Result(code, null);
}
/**
* 返回異常的
*
* @param code 狀態碼
* @param message 提示信息
* @return 返回 result
*/
public static Result error(String code, String message) {
return new Result(code, message);
}
/**
* 返回異常的
*
* @param t 返回的枚舉
* @param codeMethod 狀態的方法
* @param msgMethod 提示信息的狀態碼
* @param <T> 泛型
* @return 返回 result 對象
*/
public static <T extends Enum> Result error(T t, String codeMethod, String msgMethod, String message) {
try {
Class<?> aClass = t.getClass();
String code = aClass.getMethod(codeMethod).invoke(t).toString();
message = StringUtils.isEmpty(message) ? aClass.getMethod(msgMethod).invoke(t).toString() : message;
return new Result(code, message);
} catch (Exception e) {
return new Result(ERROR_CODE, e.getMessage());
}
}
// ---- 重寫的方法 ----
/**
* 重寫 toString 方法
*
* @return 返回JSON字符串
*/
@Override
public String toString() {
return JSON.toJSONString(this);
}
/**
* toJsonString 方法
*
* @return 返回JSON字符串
*/
public String toJsonString() {
return JSON.toJSONString(this);
}
}
StatusEnum:
public enum StatusEnum {
/**
* 操作成功
*/
OK("200", "操作成功"),
/**
* 沒有訪問權限
*/
NOT_PERMISSION("403", "沒有訪問權限"),
/**
* 沒有登錄或者登錄已經過期
*/
NOT_LOGIN("405", "沒有登錄或者登錄已經過期"),
/**
* 系統繁忙,請稍後再試
*/
SYSTEM_ERROR("500", "系統繁忙,請稍後再試");
/**
* 構造函數
*
* @param code 狀態碼
* @param message 提示信息
*/
StatusEnum(String code, String message) {
this.code = code;
this.message = message;
}
/**
* 狀態碼
*/
private String code;
/**
* 提示信息
*/
private String message;
/**
* 獲取 狀態碼
*
* @return 狀態碼
*/
public String getCode() {
return code;
}
/**
* 獲取 提示信息
*
* @return 提示信息
*/
public String getMessage() {
return message;
}
}