|
@@ -1,19 +1,3 @@
|
|
|
-/*
|
|
|
- * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
|
|
|
- *
|
|
|
- * Redistribution and use in source and binary forms, with or without
|
|
|
- * modification, are permitted provided that the following conditions are met:
|
|
|
- *
|
|
|
- * Redistributions of source code must retain the above copyright notice,
|
|
|
- * this list of conditions and the following disclaimer.
|
|
|
- * Redistributions in binary form must reproduce the above copyright
|
|
|
- * notice, this list of conditions and the following disclaimer in the
|
|
|
- * documentation and/or other materials provided with the distribution.
|
|
|
- * Neither the name of the dreamlu.net developer nor the names of its
|
|
|
- * contributors may be used to endorse or promote products derived from
|
|
|
- * this software without specific prior written permission.
|
|
|
- * Author: Chill 庄骞 (smallchill@163.com)
|
|
|
- */
|
|
|
package org.springblade.core.tool.api;
|
|
|
|
|
|
import io.swagger.annotations.ApiModel;
|
|
@@ -28,6 +12,10 @@ import org.springframework.lang.Nullable;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.Serializable;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/**
|
|
@@ -46,13 +34,16 @@ public class R<T> implements Serializable {
|
|
|
|
|
|
@ApiModelProperty(value = "状态码", required = true)
|
|
|
private int code;
|
|
|
+
|
|
|
@ApiModelProperty(value = "是否成功", required = true)
|
|
|
private boolean success;
|
|
|
- @ApiModelProperty(value = "承载数据")
|
|
|
- private T data;
|
|
|
+
|
|
|
@ApiModelProperty(value = "返回消息", required = true)
|
|
|
private String msg;
|
|
|
|
|
|
+ @ApiModelProperty(value = "承载数据")
|
|
|
+ private T data;
|
|
|
+
|
|
|
private R(IResultCode resultCode) {
|
|
|
this(resultCode, null, resultCode.getMessage());
|
|
|
}
|
|
@@ -71,9 +62,69 @@ public class R<T> implements Serializable {
|
|
|
|
|
|
private R(int code, T data, String msg) {
|
|
|
this.code = code;
|
|
|
- this.data = data;
|
|
|
this.msg = msg;
|
|
|
this.success = ResultCode.SUCCESS.code == code;
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回统一列表报文
|
|
|
+ *
|
|
|
+ * @param data 数据
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> list(List<?> data) {
|
|
|
+ Map<String, List<?>> map = new HashMap<>();
|
|
|
+ map.put("list", Optional.ofNullable(data).orElse(new ArrayList<>()));
|
|
|
+ return success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回统一数字报文
|
|
|
+ *
|
|
|
+ * @param count 数据
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> count(Integer count) {
|
|
|
+ Map<String, Integer> map = new HashMap<>();
|
|
|
+ map.put("count", Optional.ofNullable(count).orElse(0));
|
|
|
+ return success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回统一数字报文
|
|
|
+ *
|
|
|
+ * @param count 数据
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> count(Long count) {
|
|
|
+ Map<String, Long> map = new HashMap<>();
|
|
|
+ map.put("count", Optional.ofNullable(count).orElse(0L));
|
|
|
+ return success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回统一详情报文
|
|
|
+ *
|
|
|
+ * @param data 数据
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> details(Object data) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("details", Optional.ofNullable(data).orElse(new HashMap<>()));
|
|
|
+ return success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回统一id报文
|
|
|
+ *
|
|
|
+ * @param id 数据id
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> id(Object id) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("id", Optional.ofNullable(id).orElse(""));
|
|
|
+ return success(map);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -137,6 +188,26 @@ public class R<T> implements Serializable {
|
|
|
/**
|
|
|
* 返回R
|
|
|
*
|
|
|
+ * @param <T> T 泛型标记
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> success() {
|
|
|
+ return new R<>(ResultCode.SUCCESS, BladeConstant.DEFAULT_SUCCESS_MESSAGE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回R
|
|
|
+ *
|
|
|
+ * @param <T> T 泛型标记
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ public static <T> R<T> success(Object data) {
|
|
|
+ return new R(ResultCode.SUCCESS, data, BladeConstant.DEFAULT_SUCCESS_MESSAGE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回R
|
|
|
+ *
|
|
|
* @param msg 消息
|
|
|
* @param <T> T 泛型标记
|
|
|
* @return R
|
|
@@ -179,7 +250,6 @@ public class R<T> implements Serializable {
|
|
|
return new R<>(ResultCode.FAILURE, msg);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 返回R
|
|
|
*
|
|
@@ -225,12 +295,4 @@ public class R<T> implements Serializable {
|
|
|
return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : fail(BladeConstant.DEFAULT_FAILURE_MESSAGE);
|
|
|
}
|
|
|
|
|
|
- public static <T> R<T> success() {
|
|
|
- return R.success(BladeConstant.DEFAULT_SUCCESS_MESSAGE);
|
|
|
- }
|
|
|
-
|
|
|
- public static <T> R<T> success(T data) {
|
|
|
- return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE);
|
|
|
- }
|
|
|
-
|
|
|
}
|