mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(web): 提升接口文档响应类型优化扩展性
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package top.continew.starter.web.autoconfigure.response;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import org.apache.commons.lang3.reflect.TypeUtils;
|
||||
import org.springdoc.core.parsers.ReturnTypeParser;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -35,7 +36,13 @@ import java.lang.reflect.Type;
|
||||
*/
|
||||
public class ApiDocGlobalResponseHandler implements ReturnTypeParser {
|
||||
|
||||
private static final Class<R> R_TYPE = R.class;
|
||||
private final GlobalResponseProperties globalResponseProperties;
|
||||
private final Class<Object> responseClass;
|
||||
|
||||
public ApiDocGlobalResponseHandler(GlobalResponseProperties globalResponseProperties) {
|
||||
this.globalResponseProperties = globalResponseProperties;
|
||||
this.responseClass = ClassUtil.loadClass(globalResponseProperties.getResponseClassFullName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取返回类型
|
||||
@@ -51,15 +58,15 @@ public class ApiDocGlobalResponseHandler implements ReturnTypeParser {
|
||||
if (!DocUtils.hasRestControllerAnnotation(methodParameter.getContainingClass())) {
|
||||
return returnType;
|
||||
}
|
||||
// 如果为 R<T> 则直接返回
|
||||
if (returnType.getTypeName().contains("top.continew.starter.web.model.R")) {
|
||||
// 如果为响应类型,则直接返回
|
||||
if (returnType.getTypeName().contains(globalResponseProperties.getResponseClassFullName())) {
|
||||
return returnType;
|
||||
}
|
||||
// 如果是 void类型,则返回 R<Void>
|
||||
if (returnType == void.class || returnType == Void.class) {
|
||||
return TypeUtils.parameterize(R_TYPE, Void.class);
|
||||
return TypeUtils.parameterize(responseClass, Void.class);
|
||||
}
|
||||
// 返回 R<T>
|
||||
return TypeUtils.parameterize(R_TYPE, returnType);
|
||||
return TypeUtils.parameterize(responseClass, returnType);
|
||||
}
|
||||
}
|
||||
|
@@ -50,6 +50,11 @@ import java.util.Locale;
|
||||
public class GlobalResponseAutoConfiguration {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GlobalResponseAutoConfiguration.class);
|
||||
private final GlobalResponseProperties globalResponseProperties;
|
||||
|
||||
public GlobalResponseAutoConfiguration(GlobalResponseProperties globalResponseProperties) {
|
||||
this.globalResponseProperties = globalResponseProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
@@ -151,7 +156,7 @@ public class GlobalResponseAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ApiDocGlobalResponseHandler apiDocGlobalResponseHandler() {
|
||||
return new ApiDocGlobalResponseHandler();
|
||||
return new ApiDocGlobalResponseHandler(globalResponseProperties);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
Reference in New Issue
Block a user