返回Json数据 结构封装R

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("errorCode", null);
        put("message", "success");
        put("success",true);
    }

    public static R error() {
        return error(1, "操作失败");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("errorCode", code);
        r.put("message", msg);
        r.put("success",false);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("message", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public static R appok(Object o) {
        R r = new R();
        r.put("pageNum", 0);
        r.put("totalSize", 1);
        r.put("done",true);
        r.put("error",false);

        if(o!=null&&o instanceof java.util.List){
            r.put("records", o);
            return r;
        }
        if(o!=null) {
            List list = new ArrayList();
            list.add(o);
            r.put("records", list);
        }
        return r;
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}

 

posted @ 2022-02-18 15:28  菜鸟不想飞  阅读(101)  评论(0编辑  收藏  举报