java后端在不同情况下需要返回不一样的错误code和msg给前端,这个时候用枚举进行通用处理最合适。
public interface IErrorCode {
int getCode();
String getMsg();
}
public enum XXXEnum implements IErrorCode {
error16(100001, "错误1..."),
error17(100002, "错误2..."),
error18(100003, "错误3..."),
error19(100004, "错误4..."),
error20(100005, "错误5..."),
error21(100006, "错误6..."),
error22(100007, "错误7...");
private int code;
private String msg;
DetetionEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
public void setCode(int code) {
this.code = code;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public int getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
}
public enum ErrorCode implements IErrorCode {
SUCCESS(1, "成功"),
MYSQL_ERROR(100000, "MySQL访问错误"),
REDIS_ERROR(100001, "Redis访问错误"),
MONGODB_ERROR(100002, "MongoDB访问错误"),
PARAMS_ERROR(100004, "参数校验错误"),
ERROR(-1, "系统错误"),
CAS_TIMEOUT(1000, "超时无应答"),
CAS_NACK(1001, "否定应答"),
CAS_BUSY(1002, "已有相同类型指令在执行"),
CAS_OFFLINE(1003, "车辆已离线"),
USERNAME_OR_PASSWORD_ERROR(2001, "用户名或密码错误"),
ACCOUNT_EXPIRED(2002, "账号已过期"),
ACCOUNT_DISABLED(2003, "账号被禁用"),
ACCOUNT_LOCK(2004, "账号被锁定"),
CLIENT_AUTHENTICATION_FAILED(2010, "客户端认证失败"),
UNSUPPORTED_GRANT_TYPE(2011, "不支持的认证模式"),
TOKEN_INVALID(2020, "token失效!"),
NO_PERMISSION(2021, "无权限访问!"),
SERVICE_UNAVAILABLE(3000, "找不到服务"),
SERVER_EXCEPTION(9999, "服务端异常");
private int code;
private String msg;
public int getCode() {
return this.code;
}
public String getMsg() {
return this.msg;
}
private ErrorCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(
ignoreUnknown = true
)
public class BaseApiResult<T> {
private int code;
private String errMsg;
@JsonInclude(Include.NON_NULL)
private Long total;
private T body;
public static <T> BaseApiResult<T> success(T object) {
BaseApiResult<T> result = new BaseApiResult();
result.setCode(ErrorCode.SUCCESS.getCode());
result.setBody(object);
return result;
}
public static <T> BaseApiResult<T> success(long total, T object) {
BaseApiResult<T> result = new BaseApiResult();
result.setCode(ErrorCode.SUCCESS.getCode());
result.setBody(object);
result.setTotal(total);
return result;
}
public static <T> BaseApiResult<T> success() {
return success((Object)null);
}
public static <T> BaseApiResult<T> error(BaseApiResult<?> result) {
return error(result.getCode(), result.getErrMsg());
}
public static <T> BaseApiResult<T> error(IErrorCode errorCode) {
BaseApiResult<T> result = new BaseApiResult();
result.setCode(errorCode.getCode());
result.setErrMsg(errorCode.getMsg());
return result;
}
public static <T> BaseApiResult<T> error(IErrorCode errorCode, Object... args) {
String msg = String.format(errorCode.getMsg(), args);
return error(errorCode.getCode(), msg);
}
public static <T> BaseApiResult<T> error(String errMsg) {
return error(-1, errMsg);
}
public static <T> BaseApiResult<T> error(int code, String errMsg) {
BaseApiResult<T> result = new BaseApiResult();
result.setCode(code);
result.setErrMsg(errMsg);
return result;
}
public static <T> BaseApiResult<T> of(int code, String errMsg, T object) {
BaseApiResult<T> result = new BaseApiResult();
result.setCode(code);
result.setBody(object);
result.setErrMsg(errMsg);
return result;
}
public BaseApiResult() {
}
public int getCode() {
return this.code;
}
public String getErrMsg() {
return this.errMsg;
}
public Long getTotal() {
return this.total;
}
public T getBody() {
return this.body;
}
public void setCode(int code) {
this.code = code;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public void setTotal(Long total) {
this.total = total;
}
public void setBody(T body) {
this.body = body;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof BaseApiResult)) {
return false;
} else {
BaseApiResult<?> other = (BaseApiResult)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getCode() != other.getCode()) {
return false;
} else {
label49: {
Object this$errMsg = this.getErrMsg();
Object other$errMsg = other.getErrMsg();
if (this$errMsg == null) {
if (other$errMsg == null) {
break label49;
}
} else if (this$errMsg.equals(other$errMsg)) {
break label49;
}
return false;
}
Object this$total = this.getTotal();
Object other$total = other.getTotal();
if (this$total == null) {
if (other$total != null) {
return false;
}
} else if (!this$total.equals(other$total)) {
return false;
}
Object this$body = this.getBody();
Object other$body = other.getBody();
if (this$body == null) {
if (other$body != null) {
return false;
}
} else if (!this$body.equals(other$body)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof BaseApiResult;
}
public int hashCode() {
int PRIME = true;
int result = 1;
result = result * 59 + this.getCode();
Object $errMsg = this.getErrMsg();
result = result * 59 + ($errMsg == null ? 43 : $errMsg.hashCode());
Object $total = this.getTotal();
result = result * 59 + ($total == null ? 43 : $total.hashCode());
Object $body = this.getBody();
result = result * 59 + ($body == null ? 43 : $body.hashCode());
return result;
}
public String toString() {
return "BaseApiResult(code=" + this.getCode() + ", errMsg=" + this.getErrMsg() + ", total=" + this.getTotal() + ", body=" + this.getBody() + ")";
}
}
return BaseApiResult.error(XXXEnum.error1);