yuzijian пре 2 година
родитељ
комит
0a780e5121

+ 25 - 0
src/main/java/com/syjy/calculate/enum/ResponseEnum.java

@@ -0,0 +1,25 @@
+package com.syjy.calculate;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 数据信息状态枚举类
+ *
+ * @author zzy
+ * @version 1.0
+ * @since 2019/6/24 9:34
+ */
+@Getter
+@AllArgsConstructor
+public enum ResponseEnum {
+    /**
+     * 0 表示返回成功
+     */
+    SUCCESS(0, "操作成功!"),
+    FAILED(1, "操作失败!"),
+    ERROR(-1, "系统错误!");
+
+    private Integer code;
+    private String message;
+}

+ 30 - 0
src/main/java/com/syjy/calculate/exception/BusinessException.java

@@ -0,0 +1,30 @@
+package com.syjy.calculate.exception;
+
+import lombok.NoArgsConstructor;
+
+/**
+ * 业务异常
+ *
+ * @author zzy
+ * @version 2.0
+ * @since 2018/10/11 11:08
+ */
+@NoArgsConstructor
+public class BusinessException extends Exception{
+
+    public BusinessException(String message) {
+        super(message);
+    }
+
+    public BusinessException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public BusinessException(Throwable cause) {
+        super(cause);
+    }
+
+    public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}

+ 87 - 0
src/main/java/com/syjy/calculate/vo/ResponseVO.java

@@ -0,0 +1,87 @@
+package com.syjy.calculate.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.syjy.calculate.ResponseEnum;
+import com.syjy.calculate.exception.BusinessException;
+import lombok.*;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+/**
+ * 数据格式返回统一
+ *
+ * @author zzy
+ * @version 1.0
+ * @since 2019/5/10 16:34
+ */
+@ToString
+@NoArgsConstructor
+@AllArgsConstructor
+@Accessors(chain = true)
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class ResponseVO<T> implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 状态码
+     */
+    @Getter
+    @Setter
+    private Integer code;
+
+    /**
+     * 描述
+     */
+    @Getter
+    @Setter
+    private String message;
+
+    /**
+     * 数据
+     */
+    @Getter
+    @Setter
+    private T data;
+
+    public static <T> ResponseVO<T> success() {
+        return restResult(ResponseEnum.SUCCESS.getCode(), null, null);
+    }
+
+    public static <T> ResponseVO<T> success(T data) {
+        return restResult(ResponseEnum.SUCCESS.getCode(), data, ResponseEnum.SUCCESS.getMessage());
+    }
+
+    public static <T> ResponseVO<T> success(T data, String message) {
+        return restResult(ResponseEnum.SUCCESS.getCode(), data, message);
+    }
+
+    public static <T> ResponseVO<T> fail() {
+        return restResult(ResponseEnum.FAILED.getCode(), null, null);
+    }
+
+    public static <T> ResponseVO<T> fail(T data) {
+        return restResult(ResponseEnum.FAILED.getCode(), data, ResponseEnum.FAILED.getMessage());
+    }
+
+    public static <T> ResponseVO<T> fail(T data, String msg) {
+        return restResult(ResponseEnum.FAILED.getCode(), data, msg);
+    }
+
+    public static ResponseVO fail(BusinessException exception) {
+        return restResult(ResponseEnum.FAILED.getCode(), null, exception.getMessage());
+    }
+
+    public static ResponseVO error(Throwable exception) {
+        return restResult(ResponseEnum.ERROR.getCode(), null, exception.getMessage());
+    }
+
+    private static <T> ResponseVO<T> restResult(Integer code, T data, String message) {
+        ResponseVO<T> apiResult = new ResponseVO<>();
+        apiResult.setCode(code);
+        apiResult.setData(data);
+        apiResult.setMessage(message);
+        return apiResult;
+    }
+}