• restful返回 json数据的JavaBean设计


    
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.Serializable;
    import java.util.Map;
    
    public class ResultEntity implements Serializable {
    
        JSONObject jsonObject;
    
        public JSONObject getJsonObject() {
            return jsonObject;
        }
    
        public void setJsonObject(JSONObject jsonObject) {
            this.jsonObject = jsonObject;
        }
    
        public ResultEntity() {
            this.jsonObject = new JSONObject();
        }
    
        public ResultEntity(JSONObject object) {
            this.jsonObject = object;
        }
    
        public ResultEntity(int code, String message) {
            this();
            this.jsonObject.put("code", code);
            this.jsonObject.put("message", message);
        }
    
        public ResultEntity(int code, String message, String description) {
            this();
            this.jsonObject.put("code", code);
            this.jsonObject.put("message", message);
            if (description != null) {
                this.jsonObject.put("description", description);
            }
        }
    
        public ResultEntity put(String key, Object value) {
            this.jsonObject.put(key, value);
            return this;
        }
    
        public ResultEntity put(Map<String, Object> map) {
            for (String key : map.keySet()) {
                this.jsonObject.put(key, map.get(key));
            }
            return this;
        }
    
        public ResultEntity putNest(String key, Object value) {
            if (!jsonObject.containsKey("data")) {
                JSONObject dataObj = new JSONObject();
                jsonObject.put("data", dataObj);
            }
            this.jsonObject.getJSONObject("data").put(key, value);
            return this;
        }
    
        public ResultEntity putNest(Map<String, Object> map) {
            if (!jsonObject.containsKey("data")) {
                JSONObject dataObj = new JSONObject();
                jsonObject.put("data", dataObj);
            }
            for (String key : map.keySet()) {
                this.jsonObject.getJSONObject("data").put(key, map.get(key));
            }
            return this;
        }
    
        public String toJsonString() {
            return this.jsonObject.toString();
        }
    
        public Map<String, Object> totMap() {
            return this.jsonObject;
        }
    }
    

    调用示例:

    方式一:

    return new ResultEntity(0,"保存成功").putNest("name","张三").putNest("sex","男");
    

    方式二:

    Map<String,Object> map = new HashMap<String,Object>();
    map.putNest("name","张三");
    map.putNest("sex","男");
    
    return new ResultEntity(0,"保存成功").putNest(map);
    

    结果:

    {
        "code":0,
        "message":"保存成功",
        "data":{
              "name":"张三",
              "sex":"男"
        }
    }
    
  • 相关阅读:
    spark集群搭建(三台虚拟机)——kafka集群搭建(4)
    spark集群搭建(三台虚拟机)——zookeeper集群搭建(3)
    MySQL忘记密码
    [ERROR]Unable to locate package
    spark集群搭建(三台虚拟机)——hadoop集群搭建(2)
    spark集群搭建(三台虚拟机)——系统环境搭建(1)
    secureCRT连接虚拟机
    virtualBox里Ubuntu设置静态IP
    一文读懂JS中的原型和原型链(图解)
    完整原型链详细图解之JS构造函数、原型 原型链、实例化对象
  • 原文地址:https://www.cnblogs.com/silfox/p/7700989.html
Copyright © 2020-2023  润新知