工作中总结的后果集工具类,可灵便增减返回后果字段
工具类:
package com.hy.web.utils;
import java.io.Serializable;
import java.util.HashMap;
public class HyResult extends HashMap<String, Object> implements Serializable {public HyResult() { }
public HyResult(int code) {this.setCode(code);
}
public HyResult(int code, String msg) {this.setCode(code);
this.setMsg(msg);
}
public HyResult(int code, Object data) {this.setCode(code);
this.setData(data);
}
public HyResult(int code, String msg, Object data) {this.setCode(code);
this.setMsg(msg);
this.setData(data);
}
public HyResult setCode(int code) {this.put("code", code);
return this;
}
public HyResult setMsg(String msg) {this.put("msg", msg);
return this;
}
public HyResult setData(Object data) {this.put("data", data);
return this;
}
public static HyResult ok() {return new HyResult(0);
}
public static HyResult fail() {return new HyResult(1, "file");
}
public static HyResult fail(String msg) {return new HyResult(1, msg);
}
public static HyResult fail(int code, String msg) {return new HyResult(code, msg);
}
public static HyResult data(Object data) {return new HyResult(0, data);
}
}
应用:
/**
* 胜利
* @return
*/
@GetMapping("/test1")
public HyResult test1() {return HyResult.ok();
}
/**
* 失败
* @return
*/
@GetMapping("/test2")
public HyResult test2() {return HyResult.fail();
}
/**
* 数据
* @return
*/
@GetMapping("/test3")
public HyResult test3() {return HyResult.data("数据");
}