import com.alibaba.fastjson.JSON;
public class RespResult {
private int code;
private String msg;
private Object data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
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;
}
public RespResult() {
super();
}
public RespResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static String success() {
return success("请求成功", null);
}
public static String success(String msg) {
return success(msg, null);
}
public static String success(Object data) {
return success("请求成功", data);
}
//转为json格式
public static String success(String msg, Object data) {
RespResult rr = new RespResult(20000, msg, data);
return JSON.toJSONString(rr);
}
public static String error(String msg) {
return error(msg, null);
}
public static String error(Object data) {
return error("请求失败", data);
}
public static String error(String msg, Object data) {
RespResult rr = new RespResult(20002, msg, data);
return JSON.toJSONString(rr);
}
}
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONType;
import com.dxc.dxc.healthcare.platformsso.infrastructure.util.ValidUtil;
import java.util.HashMap;
/**
* 用户数据返回的实体
*/
@JSONType(naming = PropertyNamingStrategy.CamelCase)
public class JSONResult {
public final static String REDIRECT_URL_KEY = "redirect_url";
public final static Integer OK = 200;
public final static Integer code301 = 301;
public final static Integer code400 = 400;
public final static Integer code401 = 401;
public final static Integer code403 = 403;
public final static Integer code404 = 404;
public final static Integer code406 = 406;
public final static Integer code500 = 500;
public final static Integer code800 = 800;
public final static Integer code801 = 801;
public final static Integer code802 = 802;
public final static Integer code803 = 803;
public final static Integer TOKEN_AUTO_REFRESH_2000 = 2000;
public final static Integer TOKEN_MT_REFRESH_2001 = 2001;
public final static Integer USER_ERROR_10001 = 10001;
public final static Integer USER_ERROR_10002 = 10002;
public final static Integer USER_ERROR_10003 = 10003;
public final static Integer USER_ERROR_10004 = 10004;
public final static Integer USER_ERROR_10005 = 10005;
public final static Integer USER_ERROR_10006 = 10006;
public final static Integer USER_ERROR_UNLOGIN = 30002;
public final static Integer INPUT_ERROR = 40001;
public final static Integer INPUT_ERROR_UNVALID = 40002;
public final static Integer BUSINESS_ERROR = 50001;
public final static Integer USER_PULL_AGAIN = 10301;
public enum ResultCode {
OK(JSONResult.OK, "请求成功"),
code301(JSONResult.code301, "需要重定向到新的页面"),
code400(JSONResult.code400, "系统错误,操作失败,请稍候重试"),
code401(JSONResult.code401, "对不起请登录"),
code403(JSONResult.code403, "对不起,您无权访问"),
code404(JSONResult.code404, "对不起,服务未找到"),
code406(JSONResult.code406, "当前token不可接受,请刷新token"),
code500(JSONResult.code500, "服务异常,请稍后重试"),
TOKEN_AUTO_REFRESH_2000(JSONResult.TOKEN_AUTO_REFRESH_2000, "自动刷新access_token,请用新的token访问"),
TOKEN_MT_REFRESH_2001(JSONResult.TOKEN_MT_REFRESH_2001, "手动刷新access_token,请用新的token访问"),
USER_ERROR_10001(JSONResult.USER_ERROR_10001, "用户名或者密码错误"),
USER_PULL_AGAIN(JSONResult.USER_PULL_AGAIN,"重新拉取"),
INPUT_ERROR(JSONResult.INPUT_ERROR, "用户输入的参数不正确");
private Integer code;
private String name;
private ResultCode(Integer code, String name) {
this.code = code;
this.name = name;
}
public Integer getCode() {
return code;
}
}
private int code;
private String msg;
private Object data;
private Long timestamp;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public Long getTimestamp() {
return timestamp;
}
public JSONResult() {
this(null);
}
public JSONResult(Object returndata) {
this(ResultCode.OK, returndata);
}
public JSONResult(ResultCode resultCode, Object returndata) {
this.code = resultCode.code;
this.msg = resultCode.name;
this.data = returndata;
this.timestamp = System.currentTimeMillis();
}
public JSONResult(Integer resultCode, String msg) {
this.code = resultCode;
this.msg = msg;
this.data = null;
this.timestamp = System.currentTimeMillis();
}
/**
* 注意: 这个不能跟setData 和 new JSONResult(ResultCode resultCode, Object returndata)混用
*
* @param key
* @param value
* @return
*/
public JSONResult map(String key, Object value){
HashMap<String, Object> map = null;
if(data == null){
map = new HashMap<>();
}else if(data instanceof HashMap){
map = (HashMap<String, Object>) data;
}
if(!ValidUtil.isEmpty(value)){
map.put(key,value);
this.setData(map);
}
return this;
}
public JSONResult code(ResultCode resultCode){
this.code = resultCode.code;
this.msg = resultCode.name;
return this;
}
public JSONResult msg(String msg){
this.msg = msg;
return this;
}
public JSONResult data(Object data){
this.data = data;
return this;
}
public HashMap<String, Object> dataGet(){
if(this.data instanceof HashMap){
return (HashMap<String, Object>) this.data;
}
return new HashMap<>();
}
public boolean hasKeyOfData(String key){
return this.dataGet().keySet().contains(key);
}
}