eight
2024-11-07 e36c1e2363e36a69a3cc8ccbc00d28b16f926abd
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
package cn.lihu.jh.module.ecg.feign;
 
import lombok.Data;
 
import java.io.Serializable;
import java.util.List;
 
/**
 * 通用返回
 *
 * @param <T> 数据泛型
 */
@Data
public class RestApiResult<T> implements Serializable {
 
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
 
    private List<T> row;
    /**
     * 错误提示,用户可阅读
     *
     */
    private String msg;
 
    /**
     * 将传入的 result 对象,转换成另外一个泛型结果的对象
     *
     * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
     *
     * @param result 传入的 result 对象
     * @param <T>    返回的泛型
     * @return 新的 CommonResult 对象
     */
    public static <T> RestApiResult<T> error(RestApiResult<?> result) {
        return error(result.getCode(), result.getMsg());
    }
 
    public static <T> RestApiResult<T> error(Integer code, String message) {
        RestApiResult<T> result = new RestApiResult<>();
        result.code = code;
        result.msg = message;
        return result;
    }
 
    public static <T> RestApiResult<T> success(T data) {
        RestApiResult<T> result = new RestApiResult<>();
        result.code = 0;
        result.data = data;
        result.msg = "";
        return result;
    }
 
    public static <T> RestApiResult<T> success(List<T> list) {
        RestApiResult<T> result = new RestApiResult<>();
        result.code = 0;
        result.row = list;
        result.msg = "";
        return result;
    }
 
}