@Data
public class CommonResult<T> {
/**
* 结果
*/
private T data;
/**
* 状态码
*/
private Integer code;
/**
* 状态码描述
*/
private String message;
public CommonResult() {}
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
protected CommonResult(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
/**
* 成功返回结果
*
*/
public static <T> CommonResult<T> success() {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage());
}
/**
* 成功返回结果
*
* @param data 获取的数据
*/
public static <T> CommonResult<T> success(T data ) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage(), data);
}
/**
* 成功返回结果
*
* @param data 获取的数据
* @param message 提示信息
*/
public static <T> CommonResult<T> success(T data, String message) {
return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), message, data);
}
/**
* 失败返回结果
* @param errorCode 错误码
* @param message 错误信息
*/
public static <T> CommonResult<T> failed(Integer errorCode, String message) {
return new CommonResult<>(errorCode, message, null);
}
/**
* 失败返回结果
* @param message 提示信息
*/
public static <T> CommonResult<T> failed(String message) {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), message, null);
}
/**
* 权限过期
*/
public static <T> CommonResult<T> unauthorized() {
return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用户登录已过期,请重新登录!", null);
}
}
public class ExceptionCode {
public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
public static final ExceptionCode FAILED = new ExceptionCode(500, "系统异常");
private int code;
private String message;
public ExceptionCode(int code, String message) {
this.code = code;
this.message= message;
}
public ExceptionCode(String message) {
this.message = message;
}
public Integer getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
}