标签:base ret 说明 arrays 包含 class err lse serve
/** * @author: Gabriel * @date: 2020/1/17 15:00 * @description 响应体结果封装 */ public class Result<T> implements Serializable { private int status; private String msg; private T data; private Result(int status) { this.status = status; } private Result(int status, T data) { this.status = status; this.data = data; } private Result(int status, String msg, T data) { this.status = status; this.msg = msg; this.data = data; } private Result(int status, String msg) { this.status = status; this.msg = msg; } /** * 使之不在json序列化结果当中 * * @return boolean boolean */ @JsonIgnore public boolean isSuccess() { return this.status == ResultCode.SUCCESS.getCode(); } /** * Gets status. * * @return the status */ public int getStatus() { return status; } /** * Gets data. * * @return the data */ public T getData() { return data; } /** * Gets msg. * * @return the msg */ public String getMsg() { return msg; } /** * Create by success server response. * * @param <T> the type parameter * @return the server response */ public static <T> Result<T> success() { return new Result<T>(ResultCode.SUCCESS.getCode()); } /** * Create by success message server response. * * @param <T> the type parameter * @param msg the msg * @return the server response */ public static <T> Result<T> successMessage(String msg) { return new Result<T>(ResultCode.SUCCESS.getCode(), msg); } /** * Create by success server response. * * @param <T> the type parameter * @param data the data * @return the server response */ public static <T> Result<T> success(T data) { return new Result<T>(ResultCode.SUCCESS.getCode(), data); } /** * Create by success server response. * * @param <T> the type parameter * @param msg the msg * @param data the data * @return the server response */ public static <T> Result<T> success(String msg, T data) { return new Result<T>(ResultCode.SUCCESS.getCode(), msg, data); } /** * Create by error server response. * * @param <T> the type parameter * @return the server response */ public static <T> Result<T> error() { return new Result<T>(ResultCode.ERROR.getCode(), ResultCode.ERROR.getDesc()); } public static <T> Result<T> error(int errorCode, String errorMessage) { return new Result<T>(errorCode, errorMessage); } /** * Create by error message server response. * * @param <T> the type parameter * @param errorMessage the error message * @return the server response */ public static <T> Result<T> errorMessage(String errorMessage) { return new Result<T>(ResultCode.ERROR.getCode(), errorMessage); } /** * Create by error code message server response. * * @param <T> the type parameter * @param errorCode the error code * @param errorMessage the error message * @return the server response */ public static <T> Result<T> errorCodeMessage(int errorCode, String errorMessage) { return new Result<T>(errorCode, errorMessage); } /** * Create by error server response. * * @param <T> the type parameter * @param errorMessage the error message * @param data the data * @return the server response */ public static <T> Result<T> error(String errorMessage, T data) { return new Result<T>(ResultCode.ERROR.getCode(), errorMessage, data); } /** * 返回系统状态码以及相应说明 * * @param ResultCode * @param <T> * @return */ public static <T> Result<T> response(ResultCode ResultCode) { return new Result<T>(ResultCode.getCode(), ResultCode.getDesc()); } @Override public String toString() { return "Result{" + "status=" + status + ", msg=‘" + msg + ‘\‘‘ + ", data=" + data + ‘}‘; } }
响应状态码枚举
1.包含常用的响应枚举
2.包含根据code获取对应的枚举值和枚举
/** * @author: Gabriel * @date: 2020/1/17 14:40 * @description 响应码枚举类 */ @Getter @AllArgsConstructor public enum ResultCode { /** * Success response code. * * @desc 请求操作成功 */ SUCCESS(0, "操作成功!"), /** * Error response code. * * @desc 请求操作失败 */ ERROR(1, "操作失败!"), /** * Illegal argument response code. * * @desc 参数错误 */ ILLEGAL_ARGUMENT(2, "请求参数错误!"), /** * Need login response code. * * @desc 需要登录 */ NEED_LOGIN(50000, "登录超时,请重新登录!"), /** * Exception response code. * * @desc 服务器内部错误 */ EXCEPTION(500, "该请求发生异常,请稍后重试!"), /** * Sys not found error response code. */ SYS_NOT_FOUND_ERROR(404, "未找到相应资源!"), DATABASE_ERROR(6000,"数据库异常,请稍后重试!"), /** * Sys not use error response code. */ SYS_NOT_USE_ERROR(403, "资源不可用!"); private final int code; private final String desc; /** * 根据code获取枚举 * @param code * @return */ public static ResultCode getEnumByCode(int code){ return Arrays.stream(values()) .filter(x->Objects.equals(x,code)) .findFirst() .orElseThrow(() -> new RuntimeException("没有找到对应的枚举类型")); } /** * 根据code获取枚举值 * @param code * @return */ public static String getDescByCode(int code){ return Arrays.stream(values()) .filter(x->Objects.equals(x,code)) .map(ResultCode::getDesc) .findFirst() .orElseThrow(() -> new RuntimeException("没有找到对应的枚举值")); } }
标签:base ret 说明 arrays 包含 class err lse serve
原文地址:https://www.cnblogs.com/gabriel-y/p/12240204.html