zj
2024-06-03 4afe73cb84c5a609662b8b4ee20693de9b86b9a3
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
package com.nq.common;
 
import com.nq.utils.HttpRequest;
import com.nq.utils.translate.GoogleTranslateUtil;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.context.i18n.LocaleContextHolder;
 
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 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, String lange) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(), new GoogleTranslateUtil().translate(msg,lange));
    }
 
    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,String lange) {
        return new ServerResponse(ResponseCode.SUCCESS.getCode(),  new GoogleTranslateUtil().translate(msg,lange), data);
    }
 
 
    public static <T> ServerResponse<T> createByError(String msg, T data,String lange) {
        return new ServerResponse(ResponseCode.ERROR.getCode(), new GoogleTranslateUtil().translate(ResponseCode.ERROR.getMsg(), lange), data);
    }
 
    public static <T> ServerResponse<T> createByError(String lange) {
        return new ServerResponse(ResponseCode.ERROR.getCode(),new GoogleTranslateUtil().translate(ResponseCode.ERROR.getMsg(), lange));
    }
 
    public static <T> ServerResponse<T> createByErrorMsg(String errormsg,String lange) {
        return new ServerResponse(ResponseCode.ERROR.getCode(),new GoogleTranslateUtil().translate(errormsg, lange));
    }
 
    public static <T> ServerResponse<T> createByErrorCodeMsg(int errorcode, String errormsg,String lange) {
        return new ServerResponse(errorcode, new GoogleTranslateUtil().translate(errormsg, lange));
    }
}