1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| @Data public class CommonResult<T> { private long code; private String message; private T data; protected CommonResult() {
} protected CommonResult(long code, String message, T data) { this.code = code; this.message = message; this.data = data; }
/** * 请求成功返回结果 * @param data data * @param <T> type * @return success */ public static <T> CommonResult<T> success(T data) { return new CommonResult<T>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data); }
/** * 自定义消息的请求成功返回结果 * @param data data * @param message message * @param <T> type * @return success */ public static <T> CommonResult<T> success(T data, String message) { return new CommonResult<T>(ResultCodeEnum.SUCCESS.getCode(), message, data); }
/** * 为不同的请求失败的方法定义的通用方法 * @param resultCodeEnum 错误码 * @param <T> type * @return failed */ public static <T> CommonResult<T> failed(ResultCodeEnum resultCodeEnum) { return new CommonResult<T>(resultCodeEnum.getCode(), resultCodeEnum.getMessage(), null); }
/** * 自定义消息的返回结果 * @param message message * @param <T> type * @return failed */ public static <T> CommonResult<T> failed(String message) { return new CommonResult<T>(ResultCodeEnum.FAILED.getCode(), message, null); }
/** * 请求失败的返回结果 * @param <T> type * @return failed */ public static <T> CommonResult<T> failed() { return failed(ResultCodeEnum.FAILED); }
/** * 参数验证失败的返回结果 * @param <T> type * @return failed */ public static <T> CommonResult<T> validateFailed() { return failed(ResultCodeEnum.VALIDATE_FAILED); }
/** * 自定义消息的参数验证失败的返回结果 * @param message message * @param <T> type * @return failed */ public static <T> CommonResult<T> validateFailed(String message) { return new CommonResult<T>(ResultCodeEnum.VALIDATE_FAILED.getCode(), message, null); }
/** * 未登录返回结果 * @param data data * @param <T> data * @return 未登录 */ public static <T> CommonResult<T> unauthorized(T data) { return new CommonResult<T>(ResultCodeEnum.UNAUTHORIZED.getCode(), ResultCodeEnum.UNAUTHORIZED.getMessage(), data); }
/** * 未授权返回结果 * @param data data * @param <T> type * @return 未授权 */ public static <T> CommonResult<T> forbidden(T data) { return new CommonResult<T>(ResultCodeEnum.FORBIDDEN.getCode(), ResultCodeEnum.FORBIDDEN.getMessage(), data); } }
|