java全局异常统一处理
[Java] 纯文本查看 复制代码
public interface MsgGlobal {
/**
* 成功
*/
Integer SUCCESS_CODE = 0;
String SUCCESS_MSG = "成功";
/**
* 失败
*/
Integer FAILED_CODE = 10000;
String FAILED_MSG = "系统执行错误";
String JSON_PARSE_ERROR = "22001_Json 没有解析成功;请检查格式!";
}
/**
* 响应结果
**/
public class Result<T> implements Serializable {
//返回的具体数据
private T data;
//错误码
private Integer eCode;
//错误信息
private String eMsg;
//是否成功
private Boolean success;
public static <T> Result<T> failed() {
return result(MsgGlobal.FAILED_CODE, MsgGlobal.FAILED_MSG, null);
}
public static <T> Result<T> failed(String msg) {
// 错误码切割 EXECUTION_ERROR = "20001_系统执行出错"
int code = MsgGlobal.FAILED_CODE;
String msgTemp = msg;
if (!StringUtils.isEmpty(msg)) {
String[] codeMsg = StringUtils.split(msg, "_");
if (codeMsg.length > 1) {
code = Integer.valueOf(codeMsg[0]);
msgTemp = codeMsg[1];
}
}
return result(code, msgTemp, null);
}
public static <T> Result<T> failed(String msg, T data) {
int code = MsgGlobal.FAILED_CODE;
String msgTemp = msg;
if (!StringUtils.isEmpty(msg)) {
String[] codeMsg = StringUtils.split(msg, "_");
if (codeMsg.length > 1) {
code = Integer.valueOf(codeMsg[0]);
msgTemp = codeMsg[1];
}
}
return result(code, msgTemp, data);
}
public static <T> Result<T> failed(Integer code, String msg) {
return result(code, msg, null);
}
public static <T> Result<T> failed(Integer code, String msg, T data) {
return result(code, msg, data);
}
public static <T> Result<T> judge(boolean status) {
if (status) {
return success();
} else {
return failed();
}
}
private static <T> Result<T> result(Integer code, String msg, T data) {
Result<T> result = new Result<T>();
result.seteCode(code);
result.setData(data);
result.seteMsg(msg);
return result;
}
public static <T> Result<T> success(T data, Long total) {
Result<T> result = new Result();
result.seteCode(MsgGlobal.SUCCESS_CODE);
result.seteMsg(MsgGlobal.SUCCESS_MSG);
result.setData(data);
return result;
}
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> success(T data) {
String msg = MsgGlobal.SUCCESS_MSG;
Integer code = MsgGlobal.SUCCESS_CODE;
if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
msg = MsgGlobal.FAILED_MSG;
code = MsgGlobal.FAILED_CODE;
}
return result(code, msg, data);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Boolean getSuccess() {
success = eCode == 0;
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Integer geteCode() {
return eCode;
}
public void seteCode(Integer eCode) {
this.eCode = eCode;
}
public String geteMsg() {
return eMsg;
}
public void seteMsg(String eMsg) {
this.eMsg = eMsg;
}
}
/**
* 自定义异常
**/
public class BizException extends RuntimeException {
public BizException(String message){
super(message);
}
public BizException(String message, Throwable cause){
super(message, cause);
}
public BizException(Throwable cause){
super(cause);
}
public static BizException from(String errorMsg) {
return new BizException(errorMsg);
}
}
/**
* 全局异常处理
**/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public Result handleBizException(BizException e) {
log.error("业务异常,异常原因:{}", e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(Exception.class)
public Result handleException(Exception e) {
log.error("未知异常,异常原因:{}", e.getMessage(), e);
e.printStackTrace();
return Result.failed(e.getMessage());
}
@ExceptionHandler({IllegalArgumentException.class, HttpMessageNotReadableException.class})
public Result handleIllegalArgumentException(Exception e) {
log.error("非法参数异常,异常原因:{}", e.getMessage(), e);
if (e instanceof HttpMessageNotReadableException) {
return Result.failed(MsgGlobal.JSON_PARSE_ERROR);
}
log.error("非法参数异常,异常原因:{}", e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(JsonProcessingException.class)
public Result handleJsonProcessingException(JsonProcessingException e) {
log.error("Json转换异常,异常原因:{}", e.getMessage(), e);
return Result.failed(e.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("参数异常,异常原因:{}", e.getMessage(), e);
return Result.failed(e.getFieldError().getDefaultMessage());
}
}
直接捕获项目的异常,自定义异常统一处理返回友好提示语! |