diff --git a/opsli-common/src/main/java/org/opsli/common/api/ResultDto.java b/opsli-common/src/main/java/org/opsli/common/api/ResultDto.java new file mode 100644 index 00000000..444cfd80 --- /dev/null +++ b/opsli-common/src/main/java/org/opsli/common/api/ResultDto.java @@ -0,0 +1,107 @@ +package org.opsli.common.api; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.springframework.http.HttpStatus; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + + +/** + * 统一返回参数 + * + * @date 2020年5月15日10:40:54 + * @author Parker + * + * 在 Feign 的调用过程中,无法直接序列化数据 + * + * 所以要加上 @JsonProperty ,否者返回则为一个null + * + */ +public class ResultDto implements Serializable { + + /** Map 容器 */ + private final Map resultMap = new HashMap<>(3); + + /** 数据 */ + @JsonProperty("data") + private T data; + + + public ResultDto(){ + resultMap.put("success", true); + resultMap.put("code", HttpStatus.OK.value()); + resultMap.put("msg", "操作成功"); + } + + /** + * 获得编号 + * @return + */ + public int getCode() { + return (int)resultMap.get("code"); + } + + /** + * 设置编号 + * @param code + */ + public void setCode(int code) { + resultMap.put("code", code); + } + + /** + * 获得信息 + * @return + */ + public String getMsg() { + return (String)resultMap.get("msg"); + } + + /** + * 设置信息 + * @param msg + */ + public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg + resultMap.put("msg", msg); + } + + /** + * 获得状态 + * @return + */ + public boolean isSuccess() { + return (boolean)resultMap.get("success"); + } + + /** + * 设置状态 + * @param success + */ + public void setSuccess(boolean success) { + resultMap.put("success", success); + } + + + // --------------------------------- + + /** + * 设置值 + * @param value + * @return + */ + public ResultDto put(T value) { + data = value; + return this; + } + + /** + * 获得值 + * @return + */ + public T get(){ + return data; + } + +} \ No newline at end of file diff --git a/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java b/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java new file mode 100644 index 00000000..4ea4ccd2 --- /dev/null +++ b/opsli-common/src/main/java/org/opsli/common/api/ResultVo.java @@ -0,0 +1,96 @@ +package org.opsli.common.api; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.springframework.http.HttpStatus; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + + +/** + * Web统一返回参数 + * + * @date 2020年5月15日10:40:54 + * @author Parker + * + */ +public class ResultVo extends HashMap implements Serializable { + + + public ResultVo(){ + this.put("success", true); + this.put("code", HttpStatus.OK.value()); + this.put("msg", "操作成功"); + } + + /** get/set + * msg : 信息 + * code : 编码 + * success : 是否成功状态 + * */ + public String getMsg() { + return (String)this.get("msg"); + } + + public void setMsg(String msg) {//向json中添加属性,在js中访问,请调用data.msg + this.put("msg", msg); + } + + public int getCode() { + return (int)this.get("code"); + } + + public void setCode(int code) { + this.put("code", code); + } + + public boolean isSuccess() { + return (boolean)this.get("success"); + } + + public void setSuccess(boolean success) { + this.put("success", success); + } + + // ------------------------------------------- + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success(String msg) { + ResultVo j = new ResultVo(); + j.setMsg(msg); + return j; + } + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo error(String msg) { + ResultVo j = new ResultVo(); + j.setSuccess(false); + j.setMsg(msg); + return j; + } + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success(Map map) { + ResultVo restResponse = new ResultVo(); + restResponse.putAll(map); + return restResponse; + } + + @JsonIgnore//返回对象时忽略此属性 + public static ResultVo success() { + return new ResultVo(); + } + + + @Override + public ResultVo put(String key, Object value) { + super.put(key, value); + return this; + } + + public ResultVo putMap(Map m) { + super.putAll(m); + return this; + } + +} \ No newline at end of file