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 <a href="mailto:njpkhuan@gmail.com">huan</a>
* @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);
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦