• 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;
        }
    }
  • 相关阅读:
    感悟
    shadow classification
    interpolation
    c语言调试技巧
    注册博客园
    用jQuery实现图片预加载和等比例缩小,大图可以点击关闭
    焦点图,带数字显示,支持常见浏览器
    又一个jquery轮播效果,焦点图,带数字显示序号,这个可以添加对应标题在图片上,支持主流浏览器
    纯css下拉菜单,支持CSS3圆角
    Jquery 下拉菜单,可以设置颜色,支持CSS3圆角
  • 原文地址:https://www.cnblogs.com/howhy/p/16291715.html
Copyright © 2020-2023  润新知