Spring web结果json
package com.howhy.domain; import java.util.HashMap; public class R<T> { private String msg; private int code; private T data; private R(MsgCode msgCode,T data){ if(msgCode==null){ return; } this.msg=msgCode.getMsg(); this.code=msgCode.getCode(); this.data=data; } private static <T> R Ok(T data){ return new R(MsgCode.success,data); } private static R error(MsgCode msgCode){ return new R(msgCode,""); } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public T getData() { return data; } public void setData(T data) { this.data = data; } @Override public String toString() { return "{" + "msg:'" + msg + '\'' + ", code:" + code + ", data:" + data + '}'; } public static void main(String[] args) { System.out.println(R.Ok("123132")); System.out.println(R.error(MsgCode.params_empty.addArgs("userid"))); } }
##RJson
package com.howhy.domain; import java.util.HashMap; import java.util.Map; public class RJson { private String msg; private Integer code; private Map<String,Object> data=new HashMap<>(); private RJson(MsgCode msgCode){ this.msg=msgCode.getMsg(); this.code=msgCode.getCode(); } public static RJson ok(){ return new RJson(MsgCode.success); } public static RJson error(MsgCode msgCode){ return new RJson(msgCode); } public RJson data(Map data ){ this.setData(data); return this; } public RJson data(String key,Object value ){ this.data.put(key,value); return this; } public Map<String, Object> getData() { return data; } public void setData(Map<String, Object> data) { this.data = data; } @Override public String toString() { return "RJson{" + "msg='" + msg + '\'' + ", code=" + code + ", data=" + data + '}'; } public static void main(String[] args) { System.out.println(RJson.error(MsgCode.error)); HashMap<String, Object> objectObjectHashMap = new HashMap<>(); objectObjectHashMap.put("ret","33333"); System.out.println(RJson.ok().data("list","234324")); System.out.println(RJson.ok().data(objectObjectHashMap)); System.out.println(RJson.error(MsgCode.params_empty.addArgs("uid"))); } }
##MsgCode
package com.howhy.domain; public class MsgCode { private String msg; private int code; private MsgCode(String msg,int code){ this.msg=msg; this.code=code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public MsgCode addArgs(Object... args){ // int code=this.code; // String msg=String.format(this.msg,args); // return new MsgCode(msg,code); this.setMsg(String.format(this.msg,args)); return this; } public static MsgCode success=new MsgCode("ok",200); public static MsgCode error=new MsgCode("error",501); public static MsgCode params_empty=new MsgCode("%s参数不能为空",510); }
##CodeMsg
package com.howhy.domain; public enum CodeMsg { SUCCESS("0k",200),ERROR("error",501),PARAMS_EMPTY("%s不能为空",600); private String msg; private int code; private CodeMsg(String msg,int code){ this.msg=msg; this.code=code; } public CodeMsg addArgs(Object...args){ this.setMsg(String.format(this.msg,args)); return this; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } }