ResultInfo.java
4.59 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.topdraw.common;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 通用返回对象
* Created by cy on 2021/01/08.
*/
@Getter
public class ResultInfo<T> implements IResultInfo<T> {
private static final long serialVersionUID = -7313465544799377989L;
private long businessCode;
private List<T> resultSet;
private String description;
private long count;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime currentTime;
public ResultInfo(long businessCode, List<T> resultSet, String description) {
this.businessCode = businessCode;
this.resultSet = resultSet;
this.description = description;
this.count = resultSet.size();
currentTime = LocalDateTime.now();
}
public ResultInfo(long businessCode, Map<String, Object> page, String description) {
this.businessCode = businessCode;
this.resultSet = (List<T>) page.get("content");
this.count = (Long) page.get("totalElements");
this.description = description;
currentTime = LocalDateTime.now();
}
/**
* 成功返回不分页结果集
* @param resultSet
* @param <T>
* @return
*/
public static <T> ResultInfo<T> success(List<T> resultSet) {
return new ResultInfo<T>(ResultCode.SUCCESS.getCode(), resultSet, "");
}
/**
* 成功返回单一实体结果集
* @param result
* @param <T>
* @return
*/
public static <T> ResultInfo<T> success(T result) {
List<T> list = new ArrayList<>();
list.add(result);
return new ResultInfo<T>(ResultCode.SUCCESS.getCode(), list, "");
}
public static <T> ResultInfo<T> success() {
return new ResultInfo<>(ResultCode.SUCCESS.getCode(), new ArrayList<>(), "");
}
/**
* 成功返回分页结果集
* @param page
* @param <T>
* @return
*/
public static <T> ResultInfo<T> successPage(Map<String, Object> page) {
return new ResultInfo<T>(ResultCode.SUCCESS.getCode(), page, "");
}
/**
* 成功返回带描述的不分页结果集
* @param resultSet
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> success(List<T> resultSet, String description) {
return new ResultInfo<T>(ResultCode.SUCCESS.getCode(), resultSet, description);
}
/**
* 带描述的服务端处理失败返回
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> failed(String description) {
return new ResultInfo<T>(ResultCode.FAILED.getCode(), new ArrayList<>(), description);
}
/**
* 带描述的服务端处理失败返回
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> failure(String description) {
return new ResultInfo<T>(ResultCode.FAILED.getCode(), new ArrayList<>(), description);
}
/**
* 未登录或token过期的失败返回
* @param <T>
* @return
*/
public static <T> ResultInfo<T> unauthorized() {
return new ResultInfo<T>(ResultCode.UNAUTHORIZED.getCode(), new ArrayList<>(), ResultCode.UNAUTHORIZED.getDescription());
}
/**
* 未授权的失败返回
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> forbidden(String description) {
return new ResultInfo<T>(ResultCode.FORBIDDEN.getCode(), new ArrayList<>(), description);
}
/**
* 参数验证失败的返回
* @param <T>
* @return
*/
public static <T> ResultInfo<T> validateFailed() {
return new ResultInfo<T>(ResultCode.VALIDATE_FAILED.getCode(), new ArrayList<>(), ResultCode.VALIDATE_FAILED.getDescription());
}
/**
* 带描述的参数验证失败返回
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> validateFailed(String description) {
return new ResultInfo<T>(ResultCode.VALIDATE_FAILED.getCode(), new ArrayList<>(), description);
}
/**
* 请求方法错误的返回
* @param description
* @param <T>
* @return
*/
public static <T> ResultInfo<T> methodNotAllowed(String description) {
return new ResultInfo<T>(ResultCode.METHOD_NOT_ALLOWED.getCode(), new ArrayList<>(), description);
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}