mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
chore(api-doc): 优化部分代码
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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.web.autoconfigure.response;
|
||||
|
||||
import org.apache.commons.lang3.reflect.TypeUtils;
|
||||
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 全局响应处理器
|
||||
* <p>
|
||||
* 接口文档全局添加响应格式 {@link R}
|
||||
* </p>
|
||||
*
|
||||
* @author echo
|
||||
* @since 2.5.2
|
||||
*/
|
||||
public class ApiDocGlobalResponseHandler implements ReturnTypeParser {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@@ -37,7 +37,6 @@ 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;
|
||||
|
||||
/**
|
||||
* 全局响应自动配置
|
||||
@@ -145,13 +144,14 @@ public class GlobalResponseAutoConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* SpringDoc 通用响应处理 - 仅处理 doc 文档响应格式
|
||||
* SpringDoc 全局响应处理器
|
||||
*
|
||||
* @return {@link DocGenericResponseHandler }
|
||||
* @return {@link ApiDocGlobalResponseHandler }
|
||||
*/
|
||||
@Bean
|
||||
public DocGenericResponseHandler genericResponseHandler() {
|
||||
return new DocGenericResponseHandler();
|
||||
@ConditionalOnMissingBean
|
||||
public ApiDocGlobalResponseHandler apiDocGlobalResponseHandler() {
|
||||
return new ApiDocGlobalResponseHandler();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
|
@@ -1,54 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@@ -23,8 +23,6 @@ import com.feiniaojin.gracefulresponse.data.ResponseStatus;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import top.continew.starter.web.autoconfigure.response.GlobalResponseProperties;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* 响应信息
|
||||
*
|
||||
@@ -49,7 +47,7 @@ public class R<T> implements Response {
|
||||
/**
|
||||
* 状态信息
|
||||
*/
|
||||
@Schema(description = "状态信息", example = "操作成功")
|
||||
@Schema(description = "状态信息", example = "ok")
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user