package jnpf.util;
|
|
import lombok.Data;
|
|
import java.io.Serializable;
|
|
/**
|
* 通用返回结果类
|
*/
|
@Data
|
public class R<T> implements Serializable {
|
private static final long serialVersionUID = 1L;
|
|
/**
|
* 状态码
|
*/
|
private int code;
|
|
/**
|
* 消息
|
*/
|
private String msg;
|
|
/**
|
* 数据
|
*/
|
private T data;
|
|
/**
|
* 成功
|
*/
|
private boolean success;
|
|
/**
|
* 构造方法
|
*/
|
private R() {
|
}
|
|
/**
|
* 构造方法
|
*/
|
private R(int code, String msg, T data, boolean success) {
|
this.code = code;
|
this.msg = msg;
|
this.data = data;
|
this.success = success;
|
}
|
|
/**
|
* 成功返回
|
*/
|
public static <T> R<T> success() {
|
return new R<>(200, "操作成功", null, true);
|
}
|
|
/**
|
* 成功返回带数据
|
*/
|
public static <T> R<T> success(T data) {
|
return new R<>(200, "操作成功", data, true);
|
}
|
|
/**
|
* 成功返回带消息和数据
|
*/
|
public static <T> R<T> success(String msg, T data) {
|
return new R<>(200, msg, data, true);
|
}
|
|
/**
|
* 失败返回
|
*/
|
public static <T> R<T> error() {
|
return new R<>(500, "操作失败", null, false);
|
}
|
|
/**
|
* 失败返回带消息
|
*/
|
public static <T> R<T> error(String msg) {
|
return new R<>(500, msg, null, false);
|
}
|
|
/**
|
* 失败返回带状态码和消息
|
*/
|
public static <T> R<T> error(int code, String msg) {
|
return new R<>(code, msg, null, false);
|
}
|
|
/**
|
* 失败返回带状态码、消息和数据
|
*/
|
public static <T> R<T> error(int code, String msg, T data) {
|
return new R<>(code, msg, data, false);
|
}
|
}
|