feat(openapi): integrate springdoc and API integration guide
- add springdoc-openapi and expose /v3/api-docs, /v3/api-docs.yaml and Swagger UI - keep API docs responses unwrapped by replacing the global return handler - add Chinese @Tag/@Operation/@Parameter docs for file and async task controllers - add docs/api-integration.md and local export scripts for AI agent consumption - ignore local OpenAPI snapshots and force LF for shell scripts
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.imyeyu.api.config;
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* OpenAPI 接口文档配置
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-11-24
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
/**
|
||||
* 移除 timi-spring 默认的全局返回处理器,由 {@link OpenApiGlobalReturnHandler} 替代。
|
||||
*
|
||||
* <p>默认处理器会把所有响应包装为 TimiResponse,导致 SpringDoc 的接口描述无法被 Swagger UI 解析。</p>
|
||||
*
|
||||
* @return Bean 定义注册后处理器
|
||||
*/
|
||||
@Bean
|
||||
public static BeanDefinitionRegistryPostProcessor openApiGlobalReturnHandlerReplacer() {
|
||||
return new BeanDefinitionRegistryPostProcessor() {
|
||||
|
||||
@Override
|
||||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
|
||||
for (String beanName : registry.getBeanDefinitionNames()) {
|
||||
BeanDefinition definition = registry.getBeanDefinition(beanName);
|
||||
if ("globalReturnHandler".equals(beanName)
|
||||
|| "com.imyeyu.spring.util.GlobalReturnHandler".equals(definition.getBeanClassName())) {
|
||||
registry.removeBeanDefinition(beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口文档信息与认证方式
|
||||
*
|
||||
* @return OpenAPI 配置
|
||||
*/
|
||||
@Bean
|
||||
public OpenAPI timiServerOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("TimiServerAPI")
|
||||
.description("夜雨综合数据中心接口")
|
||||
.version("1.0.79"))
|
||||
.components(new Components()
|
||||
.addSecuritySchemes("Token", new SecurityScheme()
|
||||
.name("Token")
|
||||
.type(SecurityScheme.Type.APIKEY)
|
||||
.in(SecurityScheme.In.HEADER)
|
||||
.description("登录令牌或 API Key,也可通过 URL 参数 token 传递")))
|
||||
.addSecurityItem(new SecurityRequirement().addList("Token"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册字节数组消息转换器,确保接口文档以原始 JSON 输出。
|
||||
*
|
||||
* <p>项目的消息转换器配置仅注册了 Jackson 转换器,{@code byte[]} 会被序列化为 Base64 字符串,
|
||||
* 导致 Swagger UI 无法解析接口文档。</p>
|
||||
*
|
||||
* @return Web MVC 配置
|
||||
*/
|
||||
@Bean
|
||||
public WebMvcConfigurer openApiByteArrayMessageConverter() {
|
||||
return new WebMvcConfigurer() {
|
||||
|
||||
@Override
|
||||
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
converters.add(0, new ByteArrayHttpMessageConverter());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user