From 82a7e8a12a6a9b3cab269a8e55c018fe48a5e4cf Mon Sep 17 00:00:00 2001 From: winLsy <18332763730@163.com> Date: Wed, 17 Jan 2018 20:16:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=9F=E4=B8=80=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=93=8D=E5=BA=94=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/core/reponse/ServerResponse.java | 133 ++++++++++++++++++ .../fast/common/response/ServerResponse.java | 1 - 2 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/reponse/ServerResponse.java diff --git a/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/reponse/ServerResponse.java b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/reponse/ServerResponse.java new file mode 100644 index 0000000..bc4bc99 --- /dev/null +++ b/boot-starter/boot-starter-security/boot-starter-security-core/src/main/java/com/micro/fast/security/core/reponse/ServerResponse.java @@ -0,0 +1,133 @@ +package com.micro.fast.security.core.reponse; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; + +/** + * 统一服务响应类,方法返回值 + * 序列化中不包含为空值的字段 + * 0表示成功,1表示失败 + * @author lishouyu 18332763730@163.com 2017/10/02 + * @version 1.0 + * @since 1.0 + */ +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class ServerResponse { + /** + * 获取数据返回的提示数据字典码 + */ + private Integer code; + /** + * 获取数据成功或者失败的时候提示的消息 + */ + private String msg; + /** + * 返回的数据 + */ + T data; + + /** + * 所有的构造方法均为私有,不允许外部直接调用构造方法 + */ + public ServerResponse() { + + } + + private ServerResponse(Integer code) { + this(code, null, null); + } + + private ServerResponse(Integer code, String msg) { + this(code, msg, null); + } + + /** + * 为了避免T 为string的时候调用的是 上面的构造方法,需要对构造方法进行包装。构造方法设置为私有,不允许外部调用 + * + * @param code  响应代码 + * @param data  相应数据 + */ + private ServerResponse(Integer code, T data) { + this(code, null, data); + } + + private ServerResponse(Integer code, String msg, T data) { + this.code = code; + this.msg = msg; + this.data = data; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + /** + * 返回代码是小于等于0的时候表示请求数据失败,或者进行验证时失败 + * + * @return 是否成功 + */ + @JsonIgnore //使其不再json序列化结果之中 + public boolean isSuccess() { + return 0 == this.code; + } + + //请求成功 + public static ServerResponse success() { + + return new ServerResponse(0); + } + + public static ServerResponse successMsg(String msg) { + + return new ServerResponse( + 0, + msg); + } + + public static ServerResponse successData(T data) { + + return new ServerResponse(0, + data); + } + + public static ServerResponse successMsgData(String msg, T data) { + + return new ServerResponse(0, + msg, data); + } + + //请求失败 + public static ServerResponse error() { + return new ServerResponse(1); + } + + public static ServerResponse errorMsg(String msg) { + return new ServerResponse(1, msg); + } + + public static ServerResponse errorCodeMsg(Integer code, String msg) { + return new ServerResponse(code, msg); + } +} diff --git a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/response/ServerResponse.java b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/response/ServerResponse.java index 7ce87e8..cb4e86d 100644 --- a/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/response/ServerResponse.java +++ b/boot-starter/boot-starter-ssm/src/main/java/com/micro/fast/common/response/ServerResponse.java @@ -3,7 +3,6 @@ package com.micro.fast.common.response; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; /** * 统一服务响应类,方法返回值