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;
}
}