Skip to content

Commit 979a565

Browse files
committed
perf: 修改 SimpleRes 取消内部类,减少码量
1 parent e6cb89b commit 979a565

2 files changed

Lines changed: 43 additions & 48 deletions

File tree

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

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
*/
1111
public class SimpleRes {
1212

13+
private Integer code;
14+
private String msg;
15+
private Object data;
16+
1317
/**
1418
* 返回执行成功的响应
1519
*
1620
* @param data 返回的数据
1721
* @return ResBuilder
1822
*/
19-
public static ResBuilder ok(Object data) {
20-
return new ResBuilder(Reason.OK, data);
23+
public static SimpleRes ok(Object data) {
24+
return new SimpleRes(Reason.OK, data);
2125
}
2226

2327
/**
@@ -26,8 +30,8 @@ public static ResBuilder ok(Object data) {
2630
* @param data 返回的数据
2731
* @return ResBuilder
2832
*/
29-
public static ResBuilder error(Object data) {
30-
return new ResBuilder(Reason.ERROR, data);
33+
public static SimpleRes error(Object data) {
34+
return new SimpleRes(Reason.ERROR, data);
3135
}
3236

3337
/**
@@ -36,58 +40,49 @@ public static ResBuilder error(Object data) {
3640
* @param reason Reason
3741
* @return ResBuilder
3842
*/
39-
public static ResBuilder reason(Reason reason) {
40-
return new ResBuilder(reason);
43+
public static SimpleRes reason(Reason reason) {
44+
return new SimpleRes(reason);
4145
}
4246

43-
/**
44-
* 表示响应格式的内部类
45-
*/
46-
public static class ResBuilder {
47-
private Integer code;
48-
private String msg;
49-
private Object data;
50-
51-
private ResBuilder(Reason reason) {
52-
this.code = reason.getCode();
53-
this.msg = reason.getMsg();
54-
}
47+
private SimpleRes(Reason reason) {
48+
this.code = reason.getCode();
49+
this.msg = reason.getMsg();
50+
}
5551

56-
private ResBuilder(Reason reason, Object data) {
57-
this.code = reason.getCode();
58-
this.msg = reason.getMsg();
59-
this.data = data;
60-
}
52+
private SimpleRes(Reason reason, Object data) {
53+
this.code = reason.getCode();
54+
this.msg = reason.getMsg();
55+
this.data = data;
56+
}
6157

62-
public Integer getCode() {
63-
return code;
64-
}
58+
public Integer getCode() {
59+
return code;
60+
}
6561

66-
public String getMsg() {
67-
return msg;
68-
}
62+
public String getMsg() {
63+
return msg;
64+
}
6965

70-
public Object getData() {
71-
return data;
72-
}
66+
public Object getData() {
67+
return data;
68+
}
7369

74-
public ResBuilder code(Integer code) {
75-
this.code = code;
76-
return this;
77-
}
70+
public SimpleRes code(Integer code) {
71+
this.code = code;
72+
return this;
73+
}
7874

79-
public ResBuilder msg(String msg) {
80-
this.msg = msg;
81-
return this;
82-
}
75+
public SimpleRes msg(String msg) {
76+
this.msg = msg;
77+
return this;
78+
}
8379

84-
public ResBuilder data(Object data) {
85-
this.data = data;
86-
return this;
87-
}
80+
public SimpleRes data(Object data) {
81+
this.data = data;
82+
return this;
83+
}
8884

89-
public String json() {
90-
return JsonUtil.objToStr(this);
91-
}
85+
public String json() {
86+
return JsonUtil.objToStr(this);
9287
}
9388
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType
2828
HttpHeaders headers = response.getHeaders();
2929
headers.setContentType(MediaType.APPLICATION_JSON);
3030

31-
if (body instanceof SimpleRes.ResBuilder) {
31+
if (body instanceof SimpleRes) {
3232
return body;
3333
}
3434
if (body instanceof String) {

0 commit comments

Comments
 (0)