处理 SpringMVC 全局处理
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package cn.iocoder.common.framework.enums;
|
||||
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
|
||||
/**
|
||||
* 全局错误码枚举
|
||||
* 1-999 系统异常编码保留
|
||||
*
|
||||
* 一般情况下,{@link GlobalErrorCodeEnum#getCode()} ()} 使用 HTTP 响应状态码 https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status
|
||||
* 虽然说,HTTP 响应状态码作为业务使用表达能力偏弱,但是使用在系统层面还是非常不错的
|
||||
* 比较特殊的是,因为之前一直使用 0 作为成功,就不使用 200 啦。
|
||||
*/
|
||||
public enum GlobalErrorCodeEnum implements ServiceExceptionUtil.Enumerable<GlobalErrorCodeEnum> {
|
||||
|
||||
SUCCESS(0, "成功"),
|
||||
|
||||
// ========== 客户端错误段 ==========
|
||||
|
||||
BAD_REQUEST(400, "请求参数不正确"),
|
||||
UNAUTHORIZED(401, "账号未登录"),
|
||||
FORBIDDEN(403, "没有该操作权限"),
|
||||
NOT_FOUND(404, "请求未找到"),
|
||||
METHOD_NOT_ALLOWED(405, "请求方法不正确"),
|
||||
|
||||
// ========== 服务端错误段 ==========
|
||||
|
||||
INTERNAL_SERVER_ERROR(500, "系统异常"),
|
||||
;
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
GlobalErrorCodeEnum(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getGroup() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package cn.iocoder.common.framework.enums;
|
||||
|
||||
/**
|
||||
* 错误码枚举类
|
||||
*
|
||||
* 系统级异常,使用 2-001-000-000 段
|
||||
*/
|
||||
public enum SysErrorCodeEnum {
|
||||
|
||||
SYS_ERROR(2001001000, "服务端发生异常"),
|
||||
MISSING_REQUEST_PARAM_ERROR(2001001001, "参数缺失"),
|
||||
VALIDATION_REQUEST_PARAM_ERROR(2001001002, "参数校验不正确")
|
||||
;
|
||||
|
||||
private final int code;
|
||||
private final String message;
|
||||
|
||||
SysErrorCodeEnum(int code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
;
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.common.framework.vo;
|
||||
|
||||
import cn.iocoder.common.framework.enums.GlobalErrorCodeEnum;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -13,7 +14,7 @@ import java.io.Serializable;
|
||||
*/
|
||||
public final class CommonResult<T> implements Serializable {
|
||||
|
||||
private static final Integer CODE_SUCCESS = 0;
|
||||
private static final Integer CODE_SUCCESS = GlobalErrorCodeEnum.SUCCESS.getCode();
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
|
||||
Reference in New Issue
Block a user