function) {
+ if (CollUtil.isEmpty(collection) || function == null) {
+ return CollUtil.newHashSet();
+ }
+ return collection
+ .stream()
+ .map(function)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toSet());
+ }
+}
\ No newline at end of file
diff --git a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/util/EnumTypeUtils.java b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/util/EnumTypeUtils.java
deleted file mode 100644
index 5b6e04f4..00000000
--- a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/util/EnumTypeUtils.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package top.continew.starter.apidoc.util;
-
-import top.continew.starter.core.enums.BaseEnum;
-
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-
-/**
- * 枚举类型工具
- *
- * @author echo
- * @since 2.4.0
- */
-public class EnumTypeUtils {
-
- private EnumTypeUtils() {
- }
-
- /**
- * 获取枚举值类型
- *
- * @param enumClass 枚举类型
- * @return 枚举值类型
- */
- public static String getEnumValueTypeAsString(Class> enumClass) {
- try {
- // 获取枚举类实现的所有接口
- Type[] interfaces = enumClass.getGenericInterfaces();
- // 遍历所有接口
- for (Type type : interfaces) {
- // 检查接口是否为参数化类型
- if (type instanceof ParameterizedType parameterizedType) {
- // 检查接口的原始类型是否为 BaseEnum
- if (parameterizedType.getRawType() != BaseEnum.class) {
- continue;
- }
- Type actualType = parameterizedType.getActualTypeArguments()[0];
- // 检查实际类型参数是否为类类型
- if (actualType instanceof Class> actualClass) {
- if (actualClass == Integer.class) {
- return "integer";
- } else if (actualClass == Long.class) {
- return "long";
- } else if (actualClass == Double.class) {
- return "number";
- } else if (actualClass == String.class) {
- return "string";
- }
- }
- }
- }
- } catch (Exception ignored) {
- // ignored
- }
- return "string";
- }
-}
diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/GlobalResponseAutoConfiguration.java b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/GlobalResponseAutoConfiguration.java
index c04fb94a..33da3db5 100644
--- a/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/GlobalResponseAutoConfiguration.java
+++ b/continew-starter-web/src/main/java/top/continew/starter/web/autoconfigure/response/GlobalResponseAutoConfiguration.java
@@ -37,6 +37,7 @@ import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import java.util.Locale;
+import top.continew.starter.web.handler.DocGenericResponseHandler;
/**
* 全局响应自动配置
@@ -143,6 +144,16 @@ public class GlobalResponseAutoConfiguration {
return messageSource;
}
+ /**
+ * SpringDoc 通用响应处理 - 仅处理 doc 文档响应格式
+ *
+ * @return {@link DocGenericResponseHandler }
+ */
+ @Bean
+ public DocGenericResponseHandler genericResponseHandler() {
+ return new DocGenericResponseHandler();
+ }
+
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Web-Global Response' completed initialization.");
diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/handler/DocGenericResponseHandler.java b/continew-starter-web/src/main/java/top/continew/starter/web/handler/DocGenericResponseHandler.java
new file mode 100644
index 00000000..43b75202
--- /dev/null
+++ b/continew-starter-web/src/main/java/top/continew/starter/web/handler/DocGenericResponseHandler.java
@@ -0,0 +1,54 @@
+package top.continew.starter.web.handler;
+
+
+
+import org.apache.commons.lang3.reflect.TypeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springdoc.core.parsers.ReturnTypeParser;
+import org.springframework.core.MethodParameter;
+import top.continew.starter.apidoc.util.DocUtils;
+import top.continew.starter.web.model.R;
+
+import java.lang.reflect.Type;
+
+/**
+ * SpringDoc 通用响应处理程序 --仅处理 doc 文档响应格式
+ *
+ 全局添加响应格式 {@link R}
+ *
+ * @Author echo
+ * @date 2024/08/12
+ */
+public class DocGenericResponseHandler implements ReturnTypeParser {
+ private static final Logger log = LoggerFactory.getLogger(DocGenericResponseHandler.class);
+
+ private static final Class R_TYPE = R.class;
+
+ /**
+ * 获取返回类型
+ *
+ * @param methodParameter 方法参数
+ * @return {@link Type }
+ */
+ @Override
+ public Type getReturnType(MethodParameter methodParameter) {
+ // 获取返回类型
+ Type returnType = ReturnTypeParser.super.getReturnType(methodParameter);
+
+ // 判断是否具有RestController 注解
+ if (!DocUtils.hasRestControllerAnnotation(methodParameter.getContainingClass())) {
+ return returnType;
+ }
+ // 如果为R 则直接返回
+ if (returnType.getTypeName().contains("top.continew.starter.web.model.R")) {
+ return returnType;
+ }
+ // 如果是void类型,则返回R
+ if (returnType == void.class || returnType == Void.class) {
+ return TypeUtils.parameterize(R_TYPE, Void.class);
+ }
+ // 返回R
+ return TypeUtils.parameterize(R_TYPE, returnType);
+ }
+}
diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/model/R.java b/continew-starter-web/src/main/java/top/continew/starter/web/model/R.java
index 30b64b59..befcdc12 100644
--- a/continew-starter-web/src/main/java/top/continew/starter/web/model/R.java
+++ b/continew-starter-web/src/main/java/top/continew/starter/web/model/R.java
@@ -32,7 +32,7 @@ import java.util.Collections;
* @since 1.0.0
*/
@Schema(description = "响应信息")
-public class R implements Response {
+public class R implements Response {
private static final GlobalResponseProperties PROPERTIES = SpringUtil.getBean(GlobalResponseProperties.class);
private static final String DEFAULT_SUCCESS_CODE = PROPERTIES.getDefaultSuccessCode();
@@ -68,7 +68,7 @@ public class R implements Response {
* 响应数据
*/
@Schema(description = "响应数据")
- private Object data = Collections.emptyMap();
+ private T data;
public R() {
}
@@ -78,7 +78,7 @@ public class R implements Response {
this.setMsg(msg);
}
- public R(String code, String msg, Object data) {
+ public R(String code, String msg, T data) {
this(code, msg);
this.data = data;
}
@@ -97,7 +97,7 @@ public class R implements Response {
@Override
public void setPayload(Object payload) {
- this.data = payload;
+ this.data = (T)payload;
}
@Override
@@ -123,11 +123,11 @@ public class R implements Response {
this.msg = msg;
}
- public Object getData() {
+ public T getData() {
return data;
}
- public void setData(Object data) {
+ public void setData(T data) {
this.data = data;
}