|
|
@@ -0,0 +1,113 @@
|
|
|
+package com.producthunt.server.common;
|
|
|
+
|
|
|
+import java.io.Serial;
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonFormat;
|
|
|
+
|
|
|
+import lombok.Data;
|
|
|
+import lombok.EqualsAndHashCode;
|
|
|
+
|
|
|
+/**
|
|
|
+ * json 统一响应体
|
|
|
+ *
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+@Data
|
|
|
+@EqualsAndHashCode
|
|
|
+public class JsonResponse<T> implements Serializable {
|
|
|
+
|
|
|
+ @Serial
|
|
|
+ private static final long serialVersionUID = -5372948141725666083L;
|
|
|
+
|
|
|
+ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS", timezone = "GMT+8")
|
|
|
+ private Date timestamp = new Date();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否成功标志位 true-成功 false-失败
|
|
|
+ */
|
|
|
+ private Boolean success;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 响应编码
|
|
|
+ */
|
|
|
+ private String code;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 错误信息
|
|
|
+ */
|
|
|
+ private String msg;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回数据
|
|
|
+ */
|
|
|
+ private T data;
|
|
|
+
|
|
|
+ public JsonResponse(Boolean success, String code, String msg) {
|
|
|
+ this.success = success;
|
|
|
+ this.code = code;
|
|
|
+ this.msg = msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JsonResponse(Boolean success, String code, String msg, T data) {
|
|
|
+ this.success = success;
|
|
|
+ this.code = code;
|
|
|
+ this.msg = msg;
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回成功响应体
|
|
|
+ *
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> JsonResponse<T> success() {
|
|
|
+ return new JsonResponse<>(true, ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回成功响应体
|
|
|
+ *
|
|
|
+ * @param data 响应数据
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> JsonResponse<T> success(T data) {
|
|
|
+ return new JsonResponse<>(true, ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(), data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回错误响应体
|
|
|
+ *
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> JsonResponse<T> fail() {
|
|
|
+ return new JsonResponse<>(false, ResponseCodeEnum.FAIL.getCode(), ResponseCodeEnum.FAIL.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回错误响应体
|
|
|
+ *
|
|
|
+ * @param msg 错误信息
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> JsonResponse<T> fail(String msg) {
|
|
|
+ return new JsonResponse<>(false, ResponseCodeEnum.FAIL.getCode(), msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回错误响应体
|
|
|
+ *
|
|
|
+ * @param code 错误编码
|
|
|
+ * @param msg 错误信息
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> JsonResponse<T> fail(String code, String msg) {
|
|
|
+ return new JsonResponse<>(false, code, msg);
|
|
|
+ }
|
|
|
+}
|