统一响应模板

2021年03月11日 16:19

package com.fedtech.common.util.result;

import cn.hutool.json.JSONUtil;
import com.fedtech.common.constants.Result.ResponseCodeConstants;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局统一响应模板
 *
 * @author huan
 * @version 1.0.0
 * @date 2021/1/9
 */
@SuppressWarnings("rawtypes")
@Data
public class R implements Serializable {

    private static final long serialVersionUID = 6426193889384507066L;
    private Boolean status;

    private Integer code;

    private String message;

    private boolean jsonData = Boolean.FALSE;

    private Map data = new HashMap<>();

    private R() {}

    public static R success() {
        R r = new R();
        r.setStatus(true);
        r.setCode(ResponseCodeConstants.SUCCESS);
        r.setMessage("请求成功!");
        return r;
    }

    public static  R success(String message) {
        R r = new R<>();
        r.setStatus(true);
        r.setCode(ResponseCodeConstants.SUCCESS);
        r.setMessage(message);
        return r;
    }

    public static  R fail() {
        R r = new R<>();
        r.setStatus(false);
        r.setCode(ResponseCodeConstants.ERROR);
        r.setMessage("请求失败!");
        return r;
    }

    public static  R fail(String key, Object value) {
        R r = new R<>();
        r.setStatus(false);
        r.data.put(key, value);
        r.setCode(ResponseCodeConstants.ERROR);
        r.setMessage("请求失败!");
        return r;
    }

    public static  R fail(String message) {
        R r = new R<>();
        r.setStatus(false);
        r.setCode(ResponseCodeConstants.ERROR);
        r.setMessage(message);
        return r;
    }

    public static  R fail(String message, Integer code) {
        R r = new R<>();
        r.setStatus(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static  R successWithJsonData(T data) {
        R r = new R<>();
        r.setStatus(Boolean.TRUE);
        r.setCode(ResponseCodeConstants.SUCCESS);
        r.data.put("data", data);
        r.jsonData = Boolean.TRUE;
        return r;
    }

    public static  R successWithData(T data) {
        R r = new R<>();
        r.setStatus(Boolean.TRUE);
        r.setCode(ResponseCodeConstants.SUCCESS);
        r.data.put("data", data);
        return r;
    }

    public static  R failedWithJsonData(T data) {
        R r = new R<>();
        r.setStatus(Boolean.FALSE);
        r.setCode(ResponseCodeConstants.ERROR);
        r.data.put("data", data);
        r.jsonData = Boolean.TRUE;
        return r;
    }

    public static  R failedWithData(T data) {
        R r = new R<>();
        r.setStatus(Boolean.FALSE);
        r.setCode(ResponseCodeConstants.ERROR);
        r.data.put("data", data);
        return r;
    }

    public R message(String message) {
        setMessage(message);
        return this;
    }

    public R code(Integer code) {
        setCode(code);
        return this;
    }

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

    public R data(Map map) {
        setData(map);
        return this;
    }

    public String toJson() {
        if (jsonData) {
            String tem = String.valueOf(data.get("data"));
            data.remove("data");
            String jsonStr = JSONUtil.toJsonPrettyStr(this);
            return StringUtils.replace(jsonStr, jsonStr.substring(30, 38), tem);
        }
        return JSONUtil.toJsonPrettyStr(this);
    }
}

本文由博客群发一文多发等运营工具平台 OpenWrite 发布

你可能感兴趣的:(spring)