Result.java
985 Bytes
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
package com.xly.erp.common.response;
import lombok.Getter;
/**
* 统一响应包装。
* docs/04 § 1.3。
*/
@Getter
public class Result<T> {
private final int code;
private final String message;
private final T data;
private final long timestamp;
private Result(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
this.timestamp = System.currentTimeMillis();
}
public static <T> Result<T> ok(T data) {
return new Result<>(ErrorCode.OK, "操作成功", data);
}
public static Result<Void> ok() {
return new Result<>(ErrorCode.OK, "操作成功", null);
}
public static <T> Result<T> fail(int code, String message) {
return new Result<>(code, message, null);
}
@SuppressWarnings("unchecked")
public static <T> Result<T> fail(int code, String message, T data) {
return new Result<>(code, message, data);
}
}