Skip to content

Commit b192315

Browse files
committed
feat: 修改 SimpleRes,返回内容添加 path 和 error 信息
1 parent 8bb547b commit b192315

1 file changed

Lines changed: 44 additions & 2 deletions

File tree

src/main/java/info/cadecode/simple/common/response/SimpleRes.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package info.cadecode.simple.common.response;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
34
import info.cadecode.simple.common.exception.SimpleException;
45
import info.cadecode.simple.constant.ReasonEnum;
56
import info.cadecode.simple.util.JsonUtil;
7+
import info.cadecode.simple.util.MapUtil;
8+
9+
import java.util.Map;
610

711
/**
812
* @author Cade Li
@@ -13,7 +17,11 @@ public class SimpleRes {
1317

1418
private Integer code;
1519
private String msg;
20+
private String path;
21+
@JsonInclude(JsonInclude.Include.NON_NULL)
1622
private Object data;
23+
@JsonInclude(JsonInclude.Include.NON_NULL)
24+
private String error;
1725

1826
private SimpleRes(ReasonEnum reasonEnum) {
1927
this.code = reasonEnum.getCode();
@@ -63,8 +71,8 @@ public static SimpleRes ok(Object data) {
6371
* @param data 返回的数据
6472
* @return ResBuilder
6573
*/
66-
public static SimpleRes error(Object data) {
67-
return reason(ReasonEnum.ERROR).data(data);
74+
public static SimpleRes fail(Object data) {
75+
return reason(ReasonEnum.FAIL).data(data);
6876
}
6977

7078
public SimpleRes code(Integer code) {
@@ -77,15 +85,41 @@ public SimpleRes msg(String msg) {
7785
return this;
7886
}
7987

88+
public SimpleRes path(String path) {
89+
this.path = path;
90+
return this;
91+
}
92+
8093
public SimpleRes data(Object data) {
8194
this.data = data;
8295
return this;
8396
}
8497

98+
public SimpleRes error(String error) {
99+
this.error = error;
100+
return this;
101+
}
102+
85103
public String json() {
86104
return JsonUtil.objToStr(this);
87105
}
88106

107+
public Map<String, Object> map() {
108+
Map<String, Object> map = MapUtil.create()
109+
.add("code", code)
110+
.add("msg", msg)
111+
.add("path", path)
112+
.asMap();
113+
if (data != null) {
114+
map.put("data", data);
115+
}
116+
if (error != null) {
117+
map.put("error", error);
118+
}
119+
120+
return map;
121+
}
122+
89123
public Integer getCode() {
90124
return code;
91125
}
@@ -94,7 +128,15 @@ public String getMsg() {
94128
return msg;
95129
}
96130

131+
public String getPath() {
132+
return path;
133+
}
134+
97135
public Object getData() {
98136
return data;
99137
}
138+
139+
public String getError() {
140+
return error;
141+
}
100142
}

0 commit comments

Comments
 (0)