You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package com.link.domain;
// 请求是成功还是失败:ok error
// 后台希望前台弹出消息:msg
// 后台返回前台数据:Data
// JSON格式的通用响应对象,封装的就是后台返回给前台的所有信息
public class Result { public static final int ERROR = 1; public static final int OK = 200;
// 当前状态(程序员判断状态):成功、失败、未登录、没有权限
private Integer code; // 描述信息(主要是给用户看的提示信息)
private String msg; // 后台返回给前端的数据 Object, User、List<User>、Map
private Object data; // 新增:前端用于直接判断成功/失败的字段
private boolean success;
public Result() { }
public Result(Integer code) { this.code = code; this.success = code == OK; // 根据状态码自动设置success
}
public Result(Integer code, String msg) { this.code = code; this.msg = msg; this.success = code == OK; }
public Result(Integer code, Object data) { this.code = code; this.data = data; this.success = code == OK; }
public Result(Integer code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; this.success = code == OK; }
// 告诉前台成功:code
public static Result ok() { return new Result(OK); }
// 告诉前台成功:code、msg
public static Result ok(String msg) { return new Result(OK, msg); }
// 告诉前台成功:code、data
public static Result ok(Object data) { return new Result(OK, data); }
// 告诉前台成功:code、msg、data
public static Result ok(String msg, Object data) { return new Result(OK, msg, data); }
// 告诉前台失败:code
public static Result error() { return new Result(ERROR); }
// 告诉前台失败:code、msg
public static Result error(String msg) { return new Result(ERROR, msg); }
// Getter和Setter方法
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; this.success = code == OK; // 同步更新success状态
}
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
// 添加success字段的Getter(不需要Setter,通过code自动维护)
public boolean isSuccess() { return success; }}
|