nidapeng
2024-05-06 8ea9463705d26f07a279bab974769a5ceed43415
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.doumee.core.model;
 
import com.doumee.core.constants.ResponseStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.http.HttpStatus;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
 
/**
 * 接口返回对象
 * @author Eva.Caesar Liu
 * @date 2023/03/21 14:49
 */
@ApiModel("响应对象")
@Data
@AllArgsConstructor
public class ApiResponse<T> implements Serializable {
 
    @ApiModelProperty(value = "响应码")
    private int code;
 
    @ApiModelProperty(value = "请求是否成功")
    private boolean success;
 
    @ApiModelProperty(value = "错误消息")
    private String message;
 
    @ApiModelProperty(value = "数据")
    private T data;
 
    @ApiModelProperty(value = "异常消息")
    private String exception;
 
    public ApiResponse () {}
 
    /**
     * 请求成功
     */
    public static <T> ApiResponse<T> success(T data) {
        return ApiResponse.success("请求成功", data);
    }
 
    /**
     * 请求成功
     */
    public static <T> ApiResponse<T> success(String message, T data) {
        return new ApiResponse<>(HttpStatus.OK.value(), Boolean.TRUE, message, data, null);
    }
 
    /**
     * 请求失败
     */
    public static <T> ApiResponse<T> failed(String message) {
        return ApiResponse.failed(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
    }
 
    /**
     * 请求失败
     */
    public static <T> ApiResponse<T> failed(ResponseStatus status) {
        return ApiResponse.failed(status.getCode(), status.getMessage());
    }
 
    /**
     * 请求失败
     */
    public static <T> ApiResponse<T> failed(ResponseStatus status, Throwable ex) {
        return ApiResponse.failed(status.getCode(), status.getMessage(), ex);
    }
 
    /**
     * 请求失败
     */
    public static <T> ApiResponse<T> failed(Integer code, String message) {
        return ApiResponse.failed(code, message, null);
    }
 
    /**
     * 请求失败
     */
    public static <T> ApiResponse<T> failed(Integer code, String message, Throwable ex) {
        if (ex == null) {
            return new ApiResponse<>(code, Boolean.FALSE, message, null, null);
        }
        // 处理异常栈,防止过多内容导致响应内容过大
        StackTraceElement[] trace = ex.getStackTrace();
        StringBuilder exceptionStack = new StringBuilder(ex + "\n");
        for (StackTraceElement traceElement : trace) {
            exceptionStack.append("\tat ").append(traceElement).append("\n");
            if (exceptionStack.length() > 5000) {
                break;
            }
        }
        return new ApiResponse<>(code, Boolean.FALSE, message, null, exceptionStack.toString());
    }
 
 
 
    public static void outputCode(HttpServletResponse response, String code) {
        String ret ="code=" + code + "";
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println(ret);
        out.flush();
        out.close();
    }
    public static void outputCode(HttpServletResponse response, int code,String msg) {
        String ret =  "{\"code\":" + code + ",\"message\":\"" + msg + "\"}";
//        PrintWriter out = null;
        try {
            response.getOutputStream().write(ret.getBytes("UTF-8"));
//            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        out.write(ret);
//        out.flush();
//        out.close();
    }
}