自用的返回类型

返回包装类

导入依赖

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.54version>
        dependency>
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtils
{
    //默认成功
    private boolean flag = true;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    private String msg;
    private Integer code;
    private Object data;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public JsonUtils() {
    }

    public JsonUtils(boolean flag, String msg) {
        this.flag = flag;
        this.msg = msg;
    }

    public JsonUtils(boolean flag, String msg, Integer code, Object data) {
        this.flag = flag;
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public JsonUtils(boolean flag, String msg, Integer code) {
        this.flag = flag;
        this.msg = msg;
        this.code = code;
    }
    public static JsonUtils error(String msg, Integer code, Object data) {
        return new JsonUtils(false, msg, code, data);
    }
    /**
     * restful 返回
     */
    public static JsonUtils error(String msg, Integer code) {
        return new JsonUtils(false, msg, code);
    }
    public static JsonUtils error(String msg, Object data) {
        return new JsonUtils(false, msg, 400, data);
    }
    public static JsonUtils error(String msg) {
        return new JsonUtils(false, msg, 400);
    }

    public static JsonUtils success(String msg) {
        return new JsonUtils(true, msg, 200);
    }

    public static JsonUtils success(String msg, Integer code) {
        return new JsonUtils(true, msg, code);
    }
    public static JsonUtils success(String msg, Object data) {
        JsonUtils util = new JsonUtils(true, msg);
        util.setData(data);
        if (data == null){
            util.setCode(401);
        }else {
            util.setCode(200);
        }
        return util;
    }

    /**
     * java对象转json字符串
     */
    public static String object2Json(Object object){
        Object o = JSONObject.toJSON(object);
        if (o == null){
            return null;
        }
        return o.toString();
    }
    /**
     * java对象转json字符串
     */
    public static String stringify(Object object){
        Object o = JSONObject.toJSON(object);
        if (o == null){
            return null;
        }
        return o.toString();
    }

    /**
     * json 转java对象
     */
    public static Object json2Object(String json){
        try {
            return JSONObject.parseObject(json);
        }catch (Exception e){
            return JSONArray.parseArray(json);
        }
    }
    /**
     * json 转java对象
     */
    public static Object parse(String json){
        try {
            return JSONObject.parseObject(json);
        }catch (Exception e){
            return JSONArray.parseArray(json);
        }
    }
    /**
     * 从JSONObject中根据key获取value
     */
    public static Object getObj(JSONObject object, String key){
        Object o = null;
        if (object.containsKey("ids")){
            o = object.get("ids");
        }
        return o;
    }

    /**
     * 添加一个属性
     */
    public static JSONObject addOption(Object object, String key, Object value){
        JSONObject o = (JSONObject) JSONObject.toJSON(object);
        o.put(key, value);
        return o;
    }

    /**
     * 移除属性
     */
    public static JSONObject removeOption(Object object, String key){
        JSONObject o = (JSONObject) JSONObject.toJSON(object);
        o.remove(key);
        return o;
    }
}

你可能感兴趣的:(SpringBoot)