Java.控制层.响应工具类.
1 package cn.com.spdbccc.cds.index.web.base;
2
3 public class ApiResponse {
4
5 private int code;
6 private String message;
7 private Object data;
8 private boolean more;
9
10 public ApiResponse(int code, String message, Object data) {
11 this.code = code;
12 this.message = message;
13 this.data = data;
14 }
15
16 public ApiResponse() {
17
18 this.code = Status.SUCCESS.getCode();
19 this.message = Status.SUCCESS.getStandardMessage();
20 }
21
22 public int getCode() {
23 return code;
24 }
25
26 public void setCode(int code) {
27 this.code = code;
28 }
29
30 public String getMessage() {
31 return message;
32 }
33
34 public void setMessage(String message) {
35 this.message = message;
36 }
37
38 public Object getData() {
39 return data;
40 }
41
42 public void setData(Object data) {
43 this.data = data;
44 }
45
46 public boolean isMore() {
47 return more;
48 }
49
50 public void setMore(boolean more) {
51 this.more = more;
52 }
53
54 public static ApiResponse ofMessage(int code, String message) {
55 return new ApiResponse(code, message, null);
56 }
57
58 public static ApiResponse ofSuccess(Object data) {
59 return new ApiResponse(Status.SUCCESS.getCode(), Status.SUCCESS.getStandardMessage(), data);
60 }
61
62 public static ApiResponse ofStatus(Status status) {
63 return new ApiResponse(status.getCode(), status.getStandardMessage(), null);
64 }
65
66 public enum Status {
67
68 SUCCESS(200, "OK"), BAD_REQUEST(400, "Bad Request"), INTERNAL_SERVER_ERROR(500,
69 "Unknown Internal Error"), NOT_VALID_PARAM(40005,
70 "Not valid Params"), NOT_SUPPORTED_OPERATION(4006, "Operation not supported");
71
72 private int code;
73 private String standardMessage;
74
75 Status(int code, String message) {
76 this.code = code;
77 this.standardMessage = message;
78 }
79
80 public int getCode() {
81 return code;
82 }
83
84 public void setCode(int code) {
85 this.code = code;
86 }
87
88 public String getStandardMessage() {
89 return standardMessage;
90 }
91
92 public void setStandardMessage(String standardMessage) {
93 this.standardMessage = standardMessage;
94 }
95
96 }
97
98 }