Java工具类Result<T>
阅读原文时间:2023年07月28日阅读:2

枚举类:ResultCodeEnum

/**
* 统一返回结果状态信息类
*
*/
@Getter
public enum ResultCodeEnum {

SUCCESS(200,"成功"),  
FAIL(201, "失败"),  
PARAM\_ERROR( 202, "参数不正确"),  
SERVICE\_ERROR(203, "服务异常"),  
DATA\_ERROR(204, "数据异常"),  
ILLEGAL\_REQUEST(205, "非法请求"),  
REPEAT\_SUBMIT(206, "重复提交"),

LOGIN\_AUTH(208, "未登录"),  
PERMISSION(209, "没有权限"),  
CODE\_ERROR(210, "验证码错误"),  
ACCOUNT\_ERROR(211, "账号不正确"),  
LOGIN\_DISABLED\_ERROR(212, "该用户已被禁用"),  
REGISTER\_MOBILE\_ERROR(213, "手机号已被使用"),  
LOGIN\_TIMEOUT(214, "登录超时"),  
PASSWORD\_ERROR(215, "密码不正确"),  
ACCOUNT\_STOP( 216, "账号已停用"),  
NODE\_ERROR( 217, "该节点下有子节点,不可以删除"),

URL\_ENCODE\_ERROR( 220, "URL编码失败"),  
ILLEGAL\_CALLBACK\_REQUEST\_ERROR( 221, "非法回调请求"),  
FETCH\_ACCESSTOKEN\_FAILD( 222, "获取accessToken失败"),  
FETCH\_USERINFO\_ERROR( 223, "获取用户信息失败"),

PAY\_RUN(250, "支付中"),  
CANCEL\_ORDER\_FAIL(251, "取消订单失败"),  
CANCEL\_ORDER\_NO(252, "不能取消预约"),

HOSCODE\_EXIST(260, "医院编号已经存在"),  
NUMBER\_NO(261, "可预约号不足"),  
TIME\_NO(262, "当前时间不可以预约"),

SIGN\_ERROR(270, "签名错误"),  
SIGN\_NOT\_EXIST(271, "没有签名"),  
SIGN\_OVERDUE(272, "签名过期"),  
HOSPITAL\_OPEN(273, "医院未开通,暂时不能访问"),  
HOSPITAL\_NOT\_EXIST(274, "医院数据不存在,请先上传医院数据"),  
HOSPITAL\_LOCK(275, "医院被锁定,暂时不能访问"),

DATA\_SEARCH\_ERROR(280, "查询记录异常"),  
DATA\_CALCULATE\_ERROR(281, "数据计算异常"),  
DATA\_SQL\_GRAMMAR\_ERROR(282, "SQL语法异常");  
;

private Integer code;

private String message;

ResultCodeEnum(Integer code, String message) {  
    this.code = code;  
    this.message = message;  
}  

}

 工具类:Result

/**
* 全局统一返回结果类
*
*/
@Data
public class Result {

//返回码  
private Integer code;

//返回消息  
private String message;

//返回数据  
private T data;

public Result(){}

// 返回数据  
protected static <T> Result<T> build(T data) {  
    Result<T> result = new Result<T>();  
    if (data != null)  
        result.setData(data);  
    return result;  
}

public static <T> Result<T> build(T body, Integer code, String message) {  
    Result<T> result = build(body);  
    result.setCode(code);  
    result.setMessage(message);  
    return result;  
}

public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {  
    Result<T> result = build(body);  
    result.setCode(resultCodeEnum.getCode());  
    result.setMessage(resultCodeEnum.getMessage());  
    return result;  
}

public static<T> Result<T> ok(){  
    return Result.ok(null);  
}

/\*\*  
 \* 操作成功  
 \* @param data  baseCategory1List  
 \* @param <T>  
 \* @return  
 \*/  
public static<T> Result<T> ok(T data){  
    return build(data, ResultCodeEnum.SUCCESS);  
}

public static<T> Result<T> fail(){  
    return Result.fail(null);  
}

/\*\*  
 \* 操作失败  
 \* @param data  
 \* @param <T>  
 \* @return  
 \*/  
public static<T> Result<T> fail(T data){  
    return build(data, ResultCodeEnum.FAIL);  
}

public Result<T> message(String msg){  
    this.setMessage(msg);  
    return this;  
}

public Result<T> code(Integer code){  
    this.setCode(code);  
    return this;  
}  

}