Files
my-bigScreen/src/main/java/com/mini/mybigscreen/Model/Result.java
2026-03-02 13:57:22 +08:00

144 lines
3.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.mini.mybigscreen.Model;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 通用响应结果类
*/
@Data
public class Result<T> implements Serializable {
// 序列化版本号,避免序列化/反序列化异常
@Serial
private static final long serialVersionUID = 1L;
private Integer code;
// 提示信息
private String msg;
// 业务数据体
private T data;
private Result() {}
private Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
/**
* 成功返回无数据默认code=200msg=操作成功)
*/
public static <T> Result<T> success() {
return new Result<>(200, "操作成功", null);
}
/**
* 成功返回带数据默认code=200msg=操作成功)
*/
public static <T> Result<T> success(T data) {
return new Result<>(200, "操作成功", data);
}
/**
* 成功返回自定义msg默认code=200无数据
*/
public static <T> Result<T> successMsg(String msg) {
return new Result<>(200, msg, null);
}
/**
* 成功返回自定义msg+数据默认code=200
*/
public static <T> Result<T> successMsg(String msg, T data) {
return new Result<>(200, msg, data);
}
/**
* 失败返回默认code=500自定义msg无数据
*/
public static <T> Result<T> error(String msg) {
return new Result<>(500, msg, null);
}
/**
* 失败返回自定义code+msg无数据
*/
public static <T> Result<T> error(Integer code, String msg) {
return new Result<>(code, msg, null);
}
/**
* 失败返回自定义code+msg+数据,按需使用)
*/
public static <T> Result<T> error(Integer code, String msg, T data) {
return new Result<>(code, msg, data);
}
/**
* 自定义响应结果(全参数)
*/
public static <T> Result<T> custom(Integer code, String msg, T data) {
return new Result<>(code, msg, data);
}
/**
* 仅通过状态码返回默认msg为空无数据
* @param code 状态码
* @return 响应结果
*/
public static <T> Result<T> byCode(Integer code) {
return new Result<>(code, "", null);
}
/**
* 仅通过状态码+提示信息返回(无数据)
* @param code 状态码
* @param msg 提示信息
* @return 响应结果
*/
public static <T> Result<T> byCode(Integer code, String msg) {
return new Result<>(code, msg, null);
}
/**
* 仅通过状态码+数据返回默认msg为空
* @param code 状态码
* @param data 业务数据
* @return 响应结果
*/
public static <T> Result<T> byCode(Integer code, T data) {
return new Result<>(code, "", data);
}
/**
* 快捷返回400参数错误无数据
*/
public static <T> Result<T> badRequest() {
return new Result<>(400, "参数错误", null);
}
/**
* 快捷返回401未授权无数据
*/
public static <T> Result<T> unauthorized() {
return new Result<>(401, "未授权", null);
}
/**
* 快捷返回403禁止访问无数据
*/
public static <T> Result<T> forbidden() {
return new Result<>(403, "禁止访问", null);
}
/**
* 快捷返回404资源不存在无数据
*/
public static <T> Result<T> notFound() {
return new Result<>(404, "资源不存在", null);
}
}