1
zj
2024-06-03 09206aedcfdf30050123e99f2af0a192ebad1de4
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
package com.nq.common;
 
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.translate.GoogleTranslateUtil;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
 
import javax.servlet.http.HttpServletRequest;
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 static String LANG = "lang";
 
    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> createBySuccessMsg(String msg,HttpServletRequest request) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,request.getHeader(LANG)));
    }
 
    public static <T> ServerResponse<T> createBySuccess(T data) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), data);
    }
 
 
    public static <T> ServerResponse<T> createBySuccess(String data,HttpServletRequest request) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(data,request.getHeader(LANG)));
    }
 
    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> createByErrorMsg(String errormsg, HttpServletRequest request) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), new GoogleTranslateUtil().translate(errormsg,request.getHeader(LANG)));
    }
 
    public static <T> ServerResponse<T> createByErrorMsg(int errorcode,String errormsg, HttpServletRequest request) {
        return new ServerResponse(errorcode, new GoogleTranslateUtil().translate(errormsg,request.getHeader(LANG)));
    }
    public static <T> ServerResponse<T> createByErrorCodeMsg(int errorcode, String errormsg) {
        return new ServerResponse(errorcode, errormsg);
    }
}