From cd85bec274ec06563c7eb77ecd3392fb472a8bc9 Mon Sep 17 00:00:00 2001 From: zhenglm Date: Sat, 17 Jul 2021 11:58:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=94=E5=9B=9EAjaxRes?= =?UTF-8?q?ult=E5=A2=9E=E5=8A=A0=E6=B3=9B=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-common/pom.xml | 10 +- .../ruoyi/common/core/domain/AjaxResult.java | 202 ++++++++++++------ 2 files changed, 144 insertions(+), 68 deletions(-) diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index edd31141d..08ef003fd 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -34,7 +34,7 @@ org.apache.shiro shiro-core - + org.apache.shiro @@ -100,7 +100,13 @@ javax.servlet javax.servlet-api + + io.swagger + swagger-annotations + 1.6.2 + compile + - \ No newline at end of file + diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/AjaxResult.java b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/AjaxResult.java index ebd301ea5..0d7f356ab 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/AjaxResult.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/core/domain/AjaxResult.java @@ -1,46 +1,95 @@ package com.ruoyi.common.core.domain; -import java.util.HashMap; import com.ruoyi.common.utils.StringUtils; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.HashMap; /** * 操作消息提醒 * * @author ruoyi */ -public class AjaxResult extends HashMap -{ +public class AjaxResult extends HashMap { private static final long serialVersionUID = 1L; - /** 状态码 */ - public static final String CODE_TAG = "code"; + /** + * 状态码 + */ + @ApiModelProperty(value = "code : 返回代码,0表示OK,其它的都有对应问题") + public int code; - /** 返回内容 */ - public static final String MSG_TAG = "msg"; + /** + * 返回内容 + */ + @ApiModelProperty(value = "msg : 如果code!=0,错误信息") + public String msg; - /** 数据对象 */ - public static final String DATA_TAG = "data"; + @ApiModelProperty(value = "如果code!=0,message的补充信息") + private Object exception; + + /** + * 数据对象 + */ + @ApiModelProperty(value = "code为0时返回结果集") + public T data = (T) new Object(); + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public Object getException() { + return exception; + } + + public void setException(Object exception) { + this.exception = exception; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } /** * 状态类型 */ - public enum Type - { - /** 成功 */ + public enum Type { + /** + * 成功 + */ SUCCESS(0), - /** 警告 */ + /** + * 警告 + */ WARN(301), - /** 错误 */ + /** + * 错误 + */ ERROR(500); private final int value; - Type(int value) - { + Type(int value) { this.value = value; } - public int value() - { + public int value() { return this.value; } } @@ -48,49 +97,56 @@ public class AjaxResult extends HashMap /** * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。 */ - public AjaxResult() - { + public AjaxResult() { } /** * 初始化一个新创建的 AjaxResult 对象 * * @param type 状态类型 - * @param msg 返回内容 + * @param msg 返回内容 */ - public AjaxResult(Type type, String msg) - { - super.put(CODE_TAG, type.value); - super.put(MSG_TAG, msg); + public AjaxResult(Type type, String msg) { + super.put(String.valueOf(code), type.value); + super.put(msg, msg); } /** * 初始化一个新创建的 AjaxResult 对象 * * @param type 状态类型 - * @param msg 返回内容 + * @param msg 返回内容 * @param data 数据对象 */ - public AjaxResult(Type type, String msg, Object data) - { - super.put(CODE_TAG, type.value); - super.put(MSG_TAG, msg); - if (StringUtils.isNotNull(data)) - { - super.put(DATA_TAG, data); + public AjaxResult(Type type, String msg, Object data) { + super.put(String.valueOf(code), type.value); + super.put(msg, msg); + if (StringUtils.isNotNull(data)) { + super.put((String) data, data); } } + public AjaxResult(String errorMsg) { + this.msg = errorMsg; + this.code = 500; + this.data = (T) new Object(); + } + + public AjaxResult(String errorMsg, int code) { + this.msg = errorMsg; + this.code = code; + this.data = (T) new Object(); + } + /** * 方便链式调用 * - * @param key 键 + * @param key 键 * @param value 值 * @return 数据对象 */ @Override - public AjaxResult put(String key, Object value) - { + public AjaxResult put(String key, Object value) { super.put(key, value); return this; } @@ -100,9 +156,11 @@ public class AjaxResult extends HashMap * * @return 成功消息 */ - public static AjaxResult success() - { - return AjaxResult.success("操作成功"); + public static AjaxResult success() { + AjaxResult result = new AjaxResult<>(); + result.setCode(Type.SUCCESS.value); + result.setMsg("操作成功"); + return result; } /** @@ -110,9 +168,10 @@ public class AjaxResult extends HashMap * * @return 成功消息 */ - public static AjaxResult success(Object data) - { - return AjaxResult.success("操作成功", data); + public static AjaxResult success(Object data) { + AjaxResult result = AjaxResult.success(); + result.setData(data); + return result; } /** @@ -121,21 +180,24 @@ public class AjaxResult extends HashMap * @param msg 返回内容 * @return 成功消息 */ - public static AjaxResult success(String msg) - { - return AjaxResult.success(msg, null); + public static AjaxResult success(String msg) { + AjaxResult result = AjaxResult.success(); + result.setMsg(msg); + return result; } /** * 返回成功消息 * - * @param msg 返回内容 + * @param msg 返回内容 * @param data 数据对象 * @return 成功消息 */ - public static AjaxResult success(String msg, Object data) - { - return new AjaxResult(Type.SUCCESS, msg, data); + public static AjaxResult success(String msg, Object data) { + AjaxResult result = AjaxResult.success(); + result.setData(data); + result.setMsg(msg); + return result; } /** @@ -144,21 +206,24 @@ public class AjaxResult extends HashMap * @param msg 返回内容 * @return 警告消息 */ - public static AjaxResult warn(String msg) - { - return AjaxResult.warn(msg, null); + public static AjaxResult warn(String msg) { + AjaxResult result = new AjaxResult(); + result.setCode(Type.WARN.value); + result.setMsg(msg); + return result; } /** * 返回警告消息 * - * @param msg 返回内容 + * @param msg 返回内容 * @param data 数据对象 * @return 警告消息 */ - public static AjaxResult warn(String msg, Object data) - { - return new AjaxResult(Type.WARN, msg, data); + public static AjaxResult warn(String msg, Object data) { + AjaxResult result = AjaxResult.warn(msg); + result.setData(data); + return result; } /** @@ -166,9 +231,11 @@ public class AjaxResult extends HashMap * * @return */ - public static AjaxResult error() - { - return AjaxResult.error("操作失败"); + public static AjaxResult error() { + AjaxResult result = new AjaxResult<>(); + result.setCode(Type.ERROR.value); + result.setMsg("操作失败"); + return result; } /** @@ -177,20 +244,23 @@ public class AjaxResult extends HashMap * @param msg 返回内容 * @return 警告消息 */ - public static AjaxResult error(String msg) - { - return AjaxResult.error(msg, null); + public static AjaxResult error(String msg) { + AjaxResult result = AjaxResult.error(); + result.setMsg(msg); + return result; } /** * 返回错误消息 * - * @param msg 返回内容 + * @param msg 返回内容 * @param data 数据对象 * @return 警告消息 */ - public static AjaxResult error(String msg, Object data) - { - return new AjaxResult(Type.ERROR, msg, data); + public static AjaxResult error(String msg, Object data) { + AjaxResult result = AjaxResult.error(); + result.setMsg(msg); + result.setData(data); + return result; } } From 65f4e3dcf2dc02c6ead83037975d7003e6a37cff Mon Sep 17 00:00:00 2001 From: zhenglm Date: Sat, 17 Jul 2021 14:20:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=BF=94=E5=9B=9EAjaxRes?= =?UTF-8?q?ult=E5=A2=9E=E5=8A=A0=E6=B3=9B=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 15 ++++++++------- ruoyi-common/pom.xml | 1 - 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 7279a36d1..c727f71cc 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ ruoyi http://www.ruoyi.vip 若依管理系统 - + 4.6.2 UTF-8 @@ -23,6 +23,7 @@ 1.21 2.3.2 3.0.0 + 1.6.2 2.1.4 1.3.1 1.2.76 @@ -37,7 +38,7 @@ - + org.springframework.boot @@ -46,14 +47,14 @@ pom import - + com.alibaba druid-spring-boot-starter ${druid.version} - + com.github.penggle @@ -81,7 +82,7 @@ shiro-ehcache ${shiro.version} - + com.github.theborakompanioni @@ -182,7 +183,7 @@ fastjson ${fastjson.version} - + com.ruoyi @@ -276,4 +277,4 @@ - \ No newline at end of file + diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 08ef003fd..7da84a899 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -103,7 +103,6 @@ io.swagger swagger-annotations - 1.6.2 compile