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 104 105 106 107 108 109 110 111
| @Data @NoArgsConstructor @AllArgsConstructor @ApiModel("响应对象") public class Response<T> implements Serializable {
@ApiModelProperty("响应码") private String code;
@ApiModelProperty("响应消息") private String msg;
@ApiModelProperty("返回数据") private T data;
public static <T> Response<T> success() { return success(null); }
public static <T> Response<T> success(T data) { return result(ResponseCode.SUCCESS, data); }
public static <T> Response<T> failed() { return result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), null); }
public static <T> Response<T> failed(String msg) { return result(ResponseCode.ERROR.getCode(), msg, null); }
public static <T> Response<T> failed(ErrorCode responseCode) { return result(responseCode.getCode(), responseCode.getMsg(), null); }
public static <T> Response<T> failed(String code, String msg) { return result(code, msg, null); }
public static <T> Response<T> judge(boolean state) { if (state) { return success(); } else { return failed(); } }
private static <T> Response<T> result(ErrorCode resultCode, T data) { return result(resultCode.getCode(), resultCode.getMsg(), data); }
private static <T> Response<T> result(String code, String msg, T data) { return new Response<>(code, msg, data); }
}
|