|
|
package com.sunyo.wlpt.message.bus.service.response;
|
|
|
|
|
|
import com.sunyo.wlpt.message.bus.service.exception.CustomException;
|
|
|
import com.sunyo.wlpt.message.bus.service.exception.CustomExceptionType;
|
|
|
import lombok.Data;
|
|
|
|
|
|
import java.io.Serializable;
|
...
|
...
|
@@ -46,25 +48,76 @@ public class ResultJson<T> implements Serializable { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 有参,构造方法;
|
|
|
* 重载
|
|
|
* 定义有参构造器
|
|
|
*
|
|
|
* @param code 响应状态
|
|
|
* @param msg 响应消息
|
|
|
*/
|
|
|
public ResultJson(String code) {
|
|
|
this.code = code;
|
|
|
}
|
|
|
|
|
|
public ResultJson(T data) {
|
|
|
this.data = data;
|
|
|
}
|
|
|
|
|
|
public ResultJson(String code, String msg) {
|
|
|
this.code = code;
|
|
|
this.msg = msg;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 定义有参构造器
|
|
|
*
|
|
|
* @param code 响应状态
|
|
|
* @param msg 响应消息
|
|
|
* @param data 响应数据
|
|
|
*/
|
|
|
public ResultJson(String code, String msg, T data) {
|
|
|
this.code = code;
|
|
|
this.msg = msg;
|
|
|
this.data = data;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 定义静态、成功方法(重载)
|
|
|
*
|
|
|
* @return 成功(没有响应数据)
|
|
|
*/
|
|
|
public static ResultJson success() {
|
|
|
return new ResultJson<>("200", "success");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 定义静态、成功方法(重载)
|
|
|
*
|
|
|
* @return 成功(响应数据)
|
|
|
*/
|
|
|
public static ResultJson success(Object data) {
|
|
|
return new ResultJson<>("200", "success", data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 请求出现异常时的响应数据封装
|
|
|
*
|
|
|
* @param e 自定义异常类
|
|
|
* @return 返回异常信息
|
|
|
*/
|
|
|
public static ResultJson error(CustomException e) {
|
|
|
ResultJson result = new ResultJson<>();
|
|
|
result.setCode(e.getCode());
|
|
|
if (e.getCode() == CustomExceptionType.CLIENT_ERROR.getCode()) {
|
|
|
result.setMsg(e.getMessage());
|
|
|
} else if (e.getCode() == CustomExceptionType.SYSTEM_ERROR.getCode()) {
|
|
|
result.setMsg(e.getMessage() + ";请将该异常发送给管理员");
|
|
|
} else {
|
|
|
result.setMsg("系统出现未知异常,请联系管理员!");
|
|
|
}
|
|
|
// 可以尝试着做异常信息持久化
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 请求出现异常时的响应数据封装
|
|
|
*
|
|
|
* @param customExceptionType 自定义异常枚举中的异常
|
|
|
* @param errorMessage 错误信息
|
|
|
* @return 返回异常信息
|
|
|
*/
|
|
|
public static ResultJson error(CustomExceptionType customExceptionType, String errorMessage) {
|
|
|
return new ResultJson<>(customExceptionType.getCode(), errorMessage);
|
|
|
}
|
|
|
} |
...
|
...
|
|