Python版本
return_util.py
def return_ok(code="200", msg="操作成功", data=None):
"""
创建 业务函数/方法的(统一的)返回结构
:param code: 状态码:"200"-操作成功, "500"-操作失败
:param msg: 消息语
:param data: 返回数据
:return:
"""
if data is None:
data = {}
return {
"code": code,
"msg": msg,
"data": data
}
def return_error(code="500", msg="操作失败", data=None):
"""
创建 业务函数/方法的(统一的)返回结构
:param code: 状态码:"200"-操作成功, "500"-操作失败
:param msg: 消息语
:param data: 返回数据
:return:
"""
if data is None:
data = {}
return {
"code": code,
"msg": msg,
"data": data
}
Java版本
package com.wanma.framework_api.model;
import cn.hutool.core.map.MapUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanma.framework_api.exception.ErrorEnum;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 业务结果类
*/
public class ResJson<T> {
private static final String CODE_OK = "200";
private static final String CODE_ERROR = "500";
private static final String MSG_OK = "操作成功";
private static final String MSG_ERROR = "操作失败";
private String code = "";
private String msg = "";
private Map<String, T> data = new LinkedHashMap<>();
public static <T> ResJson<T> instance() {
return new ResJson<>();
}
public String getCode() {
return this.code;
}
public ResJson<T> setCode(String code) {
this.code = code;
return this;
}
public String getMsg() {
return this.msg;
}
public ResJson<T> setMsg(String msg) {
this.msg = msg;
return this;
}
public Map<String, T> getData() {
return this.data;
}
public String getStr(Object key) {
try {
return MapUtil.getStr(this.data, key, "");
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public Integer getInt(Object key) {
try {
return MapUtil.getInt(this.data, key, 0);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
public Long getLong(Object key) {
try {
return MapUtil.getLong(this.data, key, 0L);
} catch (Exception e) {
e.printStackTrace();
return 0L;
}
}
public ResJson<T> setData(Map<String, T> data) {
this.data = data;
return this;
}
public ResJson<T> setData(String key, T value) {
this.data.put(key, value);
return this;
}
public ResJson() {
}
public ResJson<T> ok() {
this.code = ResJson.CODE_OK;
if (this.msg.equals("") || this.msg.equals(ResJson.MSG_ERROR)) {
this.msg = ResJson.MSG_OK;
}
return this;
}
public ResJson<T> ok(String msg) {
this.code = ResJson.CODE_OK;
this.msg = msg;
return this;
}
public ResJson<T> ok(Map<String, T> data) {
this.code = ResJson.CODE_OK;
this.data = data;
if (this.msg.equals("") || this.msg.equals(ResJson.MSG_ERROR)) {
this.msg = ResJson.MSG_OK;
}
return this;
}
public ResJson<T> ok(String msg, Map<String, T> data) {
this.code = ResJson.CODE_OK;
this.msg = msg;
this.data = data;
return this;
}
public ResJson<T> ok(String code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResJson<T> ok(String code, String msg, Map<String, T> data) {
this.code = code;
this.msg = msg;
this.data = data;
return this;
}
public ResJson<T> error() {
this.code = ResJson.CODE_ERROR;
if (this.msg.equals("") || this.msg.equals(ResJson.MSG_OK)) {
this.msg = ResJson.MSG_ERROR;
}
return this;
}
public ResJson<T> error(String msg) {
this.code = ResJson.CODE_ERROR;
this.msg = msg;
return this;
}
public ResJson<T> error(Map<String, T> data) {
this.code = ResJson.CODE_ERROR;
this.data = data;
if (this.msg.equals("") || this.msg.equals(ResJson.MSG_OK)) {
this.msg = ResJson.MSG_ERROR;
}
return this;
}
public ResJson<T> error(String msg, Map<String, T> data) {
this.code = ResJson.CODE_ERROR;
this.msg = msg;
this.data = data;
return this;
}
public ResJson<T> error(String code, String msg) {
this.code = code;
this.msg = msg;
return this;
}
public ResJson<T> error(String code, String msg, Map<String, T> data) {
this.code = code;
this.msg = msg;
this.data = data;
return this;
}
public ResJson<T> error(ErrorEnum errorEnum) {
this.code = errorEnum.getErrorCode();
this.msg = errorEnum.getErrorMsg();
return this;
}
@JsonIgnore
public boolean isOk() {
return this.code.equals(CODE_OK);
}
@JsonIgnore
public boolean isError() {
return !this.isOk();
}
/**
* 输出JSON字符串
*/
@JsonIgnore
public String outJson() {
Map<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("code", this.code);
jsonMap.put("msg", this.msg);
jsonMap.put("data", this.data);
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(jsonMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "{\"code\":\"" + this.getCode() + "\",\"msg\":\"" + this.getMsg() + "\", \"data\": \"\"}";
}
}
参考: