import cn.hutool.core.date.DateUtil; public class R { private boolean success = true;//成功还是失败 private Integer code = 200;//状态码 private String message = "";//信息 private Object resultObj = null;//数据 private String timestamp = DateUtil.now();//时间戳 yyyy-MM-dd HH:mm:ss //实例对象 public static R INSTANCE = new R(); //成功返回 public R ok(){ return this; } public R ok(Object resultObj){ this.resultObj = resultObj; return this; } //失败返回 public R error(String message){ this.success = false; this.code = 500;//默认错误码500 this.message = message; return this; } public R error(Integer code,String message){ this.success = false; this.code = code; this.message = message; return this; } public boolean isSuccess() { return success; } public R setSuccess(boolean success) { this.success = success; return this; } public Integer getCode() { return code; } public R setCode(Integer code) { this.code = code; return this; } public String getMessage() { return message; } public R setMessage(String message) { this.message = message; return this; } public Object getResultObj() { return resultObj; } public R setResultObj(Object resultObj) { this.resultObj = resultObj; return this; } @Override public String toString() { return "R{" + "success=" + success + ", code=" + code + ", message='" + message + ''' + ", resultObj=" + resultObj + ", timestamp='" + timestamp + ''' + '}'; } }
注:
日期字段默认系统当前时间yyyy-MM-dd HH:mm:ss格式,依赖于hutool工具包,也可以使用自己的写法