【二三方】_统一响应体类ResponseDTO构建

统一响应体类ResponseDTO构建

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
112
113
114
115
116
117
118
119
120
package jingzhao;

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class ResponseDTO<T> implements Serializable {

private static final long serialVersionUID = 1374862392366780629L;

/**
* 接口调用成功/失败
*/
private Boolean success;

/**
* 返回信息
*/
private String msg;

/**
* 返回code
*/
private String code;

/**
* 返回业务结果数据
*/
private T data;

/**
* 原因描述
*/
private String description;

/**
* 错误map
*/
private Map<String, String> errorMap;

public static <T> ResponseDTO<T> successData(T object) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.TRUE;
responseDTO.setData(object);
return responseDTO;
}

//若某类或某方法加上@Deprecated注解,表示此方法或类不再建议使用,调用时也会出现删除线,但并不代表不能用。
@Deprecated
public static <T> ResponseDTO<T> success(T object) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.TRUE;
responseDTO.setData(object);
return responseDTO;
}

public static <T> ResponseDTO<T> success(T object, String msg) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.TRUE;
responseDTO.setData(object);
responseDTO.setMsg(msg);
return responseDTO;
}

public static <T> ResponseDTO<T> success(String msg) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.TRUE;
responseDTO.setMsg(msg);
return responseDTO;
}

public static <T> ResponseDTO<T> bizError(String msg, String code) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.FALSE;
responseDTO.setMsg(msg);
responseDTO.setCode("BIZ_ERROR, " + code);
return responseDTO;
}

public static <T> ResponseDTO<T> bizError(String msg) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.success = Boolean.FALSE;
responseDTO.setMsg(msg);
responseDTO.setCode("BIZ_ERROR");
return responseDTO;
}

public static <T> ResponseDTO<T> bizErrorWithData(String msg, String code, T data) {
ResponseDTO<T> responseDTO = new ResponseDTO<>();
responseDTO.setSuccess(false);
responseDTO.setMsg(msg);
responseDTO.setData(data);
responseDTO.setCode(code);
return responseDTO;
}

public static <T> ResponseDTO<T> response(Boolean isSuccess, String errorCode
, String errorMSg, String description, T data) {
ResponseDTO responseDTO = new ResponseDTO();
responseDTO.setSuccess(isSuccess);
responseDTO.setCode(errorCode);
responseDTO.setMsg(errorMSg);
responseDTO.setDescription(description);
responseDTO.setData(data);
return responseDTO;
}

public Boolean isSuccess() {
return success;
}

public Integer getSucc() {
if (success == null) {
return 0;
}
return success ? 1 : 0;
}
}
  • public static <T> T

    对于声明了泛型<T>的类(比如此处的ResponseDTO<T>),其中的普通方法不需要再进行泛型声明,如isSuccess()和getSucc();对于带了static的静态方法,则需要再次声明为泛型方法,具体写法即为在static后添加一个<T>。