zj
2024-06-03 3cab39d85e5e43b8cc95ab82c9a914386b71c999
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
package org.example.common;
 
 
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 
import java.io.Serializable;
 
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServerResponse<T> extends Object implements Serializable {
    private int status;
    private String msg;
    private T data;
 
    private ServerResponse(int status) {
        this.status = status;
    }
 
    private ServerResponse(int status, T data) {
        this.status = status;
        this.data = data;
    }
 
    private ServerResponse(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }
 
    private ServerResponse(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
 
    public static void main(String[] args) {
        ServerResponse serverResponse = new ServerResponse(1, new Object());
        ServerResponse serverResponse1 = new ServerResponse(1, "abc");
        System.out.print("ServerResponse");
    }
 
    @JsonIgnore
    public boolean isSuccess() {
        return (this.status == ResponseCode.SUCCESS.getCode());
    }
 
    public int getStatus() {
        return this.status;
    }
 
    public T getData() {
        return (T) this.data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
 
    public String getMsg() {
        return this.msg;
    }
 
    public static <T> ServerResponse<T> createBySuccess() {
        return new ServerResponse(ResponseCode.SUCCESS.getCode());
    }
 
    public static <T> ServerResponse<T> createBySuccessMsg(String msg) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg);
    }
 
    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), data);
    }
 
    public static <T> ServerResponse<T> createBySuccess(String msg, T data) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), msg, data);
    }
 
 
    public static <T> ServerResponse<T> createByError(String msg, T data) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), msg, data);
    }
 
    public static <T> ServerResponse<T> createByError() {
        return new ServerResponse(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg());
    }
 
    public static <T> ServerResponse<T> createByErrorMsg(String errormsg) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), errormsg);
    }
 
    public static <T> ServerResponse<T> createByErrorCodeMsg(int errorcode, String errormsg) {
        return new ServerResponse(errorcode, errormsg);
    }
}