import lombok.Data;
/**
* 自定义响应结构
*/
@Data
public class ReturnResult<T> {
private int code;
private String msg;
private T data;
public static <T> ReturnResult<T> success() {
ReturnResult result = new ReturnResult<T>();
result.setCode(200);
result.setMsg("成功");
return result;
}
public static <T> ReturnResult<T> success(T data) {
ReturnResult result = new ReturnResult<T>(data);
result.setCode(200);
result.setMsg("成功");
return result;
}
public static <T> ReturnResult<T> error() {
ReturnResult result = new ReturnResult<T>();
result.setCode(2000);
result.setMsg("服务端异常");
return result;
}
public static <T> ReturnResult<T> error(String msg) {
ReturnResult result = new ReturnResult<T>(msg);
result.setCode(2000);
return result;
}
public static <T> ReturnResult<T> error(Integer code, String msg) {
return new ReturnResult<T>(code, msg);
}
public static <T> ReturnResult<T> error(Integer code, String msg, T data) {
return new ReturnResult<T>(code, msg, data);
}
private ReturnResult() {
}
private ReturnResult(String msg) {
this.msg = msg;
this.data = null;
}
private ReturnResult(T data) {
this.data = data;
}
private ReturnResult(Integer code, String msg) {
this.code = code;
this.msg = msg;
this.data = null;
}
private ReturnResult(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
Result类(返回类)
于 2023-05-24 16:27:11 首次发布
该文章展示了一个使用Lombok库的@Data注解创建的Java类ReturnResult,用于构建统一的API响应。类包含了状态码(code),消息(msg)和数据(data)字段,并提供了多个静态工厂方法来方便创建成功或错误的响应实例。
1690

被折叠的 条评论
为什么被折叠?



