mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(api-doc/web): 重构doc文档枚举展示处理,处理doc 文档全局响应格式
This commit is contained in:
@@ -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.");
|
||||
|
@@ -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 文档响应格式
|
||||
* <p>
|
||||
全局添加响应格式 {@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> 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<T> 则直接返回
|
||||
if (returnType.getTypeName().contains("top.continew.starter.web.model.R")) {
|
||||
return returnType;
|
||||
}
|
||||
// 如果是void类型,则返回R<Void>
|
||||
if (returnType == void.class || returnType == Void.class) {
|
||||
return TypeUtils.parameterize(R_TYPE, Void.class);
|
||||
}
|
||||
// 返回R<T>
|
||||
return TypeUtils.parameterize(R_TYPE, returnType);
|
||||
}
|
||||
}
|
@@ -32,7 +32,7 @@ import java.util.Collections;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Schema(description = "响应信息")
|
||||
public class R implements Response {
|
||||
public class R<T> 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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user