当前位置: 首页 > news >正文

SpringCloud + React19 集成Scalar的API文档

你好呀,我的老朋友!我是老寇,跟我一起学习集成Scalr API文档

众所周知,Knife4j是一个集Swagger2OpenAPI3为一体的增强解决方案,但是社区不太活跃,再加上SpringBoot4发版,迫切需要一个集成OpenAPI 3.0OpenAPI 3.1规范的增强解决方案,所以才集成Scalar

React集成Scalar

React集成Scalar文档地址

安装依赖
pnpminstall@scalar/api-reference-react
配置详情
配置项类型是否必填说明示例
sourcesArrayOpenAPI 文档源列表,一个配置可以配置多个 API 文档。[{ title: "认证授权", url: "/api/v3/api-docs" }]
sources.titleStringAPI 文档名称,在 Scalar 左侧导航中显示。"认证授权"
sources.urlStringOpenAPI(Swagger)JSON 文档地址。"/api-proxy/auth/api/v3/api-docs"
sources.defaultBoolean是否默认选中该文档。true
proxyUrlStringScalar 的代理地址,用于 Try It、鉴权等请求代理。"/api-proxy/auth/scalar"
expandAllResponsesBoolean是否默认展开所有 Response。true
hideClientButtonBoolean是否隐藏 Generate Client(生成客户端)按钮。true
orderRequiredPropertiesFirstBooleanModel 属性中,是否将必填字段排在前面。true
expandAllModelSectionsBoolean是否默认展开所有 Model 定义。false
具体代码

apiDoc.tsx

import{ApiReferenceReact}from'@scalar/api-reference-react'import'@scalar/api-reference-react/style.css'import{useIntl}from'@@/exports';exportdefault()=>{constintl=useIntl();constt=(id:string,values?:Record<string,any>)=>intl.formatMessage({id},values);return(<ApiReferenceReact configuration={[{sources:[{title:'认证授权',url:"/api-proxy/auth/api/v3/api-docs",default:true,}],proxyUrl:"/api-proxy/auth/scalar",expandAllResponses:true,hideClientButton:true,orderRequiredPropertiesFirst:true,expandAllModelSections:false,},{sources:[{title:'后台管理',url:"/api-proxy/admin/api/v3/api-docs",default:true,}],proxyUrl:"/api-proxy/admin/scalar",expandAllResponses:true,hideClientButton:true,orderRequiredPropertiesFirst:true,expandAllModelSections:false,}]}/>);};

Spring Cloud Gateway集成Scalar

由于代理地址为/**/scalar因此,需要在网关层将/**/scalar覆盖为scalar_url

依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway-server-webflux</artifactId> </dependency>
具体代码

ScalarGatewayFilterFactory

@Slf4j@ComponentpublicclassScalarGatewayFilterFactoryextendsAbstractGatewayFilterFactory<ScalarGatewayFilterFactory.@NonNullConfig>implementsOrdered{publicScalarGatewayFilterFactory(){super(Config.class);}@OverridepublicintgetOrder(){returnOrdered.LOWEST_PRECEDENCE-2000;}@NonNull@OverridepublicGatewayFilterapply(Configconfig){return(exchange,chain)->{ServerHttpRequestrequest=exchange.getRequest();// 获取uriStringrequestURL=ReactiveRequestUtils.getRequestURL(request);// scalar重写地址if(ReactiveRequestUtils.pathMatcher("/**/scalar",requestURL)){StringscalarUrl=ReactiveRequestUtils.getParamValue(request,"scalar_url");URIuri=URI.create(scalarUrl);returnchain.filter(exchange.mutate().request(request.mutate().uri(uri).path(uri.getPath()).build()).build());}returnchain.filter(exchange);};}publicstaticclassConfig{}}

application.yml

spring:cloud:gateway:server:webflux:enabled:trueroutes:-id:laokou-auth-scalaruri:lb://laokou-authpredicates:-Path=/api-gateway/auth/scalarfilters:-name:Scalar-id:laokou-admin-scalaruri:lb://laokou-adminpredicates:-Path=/api-gateway/admin/scalarfilters:-name:Scalar

SpringBoot集成Scalar(以laokou-auth为例)

安装依赖
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-scalar</artifactId></dependency>
具体代码

OpenApiDocConfig

@ConfigurationpublicclassOpenApiDocConfig{@BeanOpenAPIopenApi(){returnnewOpenAPI().info(newInfo().title("API文档").description("API文档").version("1.0.0").contact(newContact().name("laokou").url("https://github.com/KouShenhai").email("2413176044@qq.com")).license(newLicense().name("Apache 2.0").url("https://www.apache.org/licenses/LICENSE-2.0.html"))).externalDocs(newExternalDocumentation().description("老寇IoT云平台").url("https://github.com/KouShenhai")).addSecurityItem(newSecurityRequirement().addList(HttpHeaders.AUTHORIZATION)).components(newComponents().addSecuritySchemes(HttpHeaders.AUTHORIZATION,newSecurityScheme().name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.OAUTH2).in(SecurityScheme.In.HEADER).scheme("Bearer").bearerFormat("JWT")));}}

application.yml

scalar:enabled:truespringdoc:swagger-ui:enabled:falseapi-docs:enabled:truepath:/v3/api-docsversion:openapi_3_1

我是老寇,我们下次再见啦!

http://www.jsqmd.com/news/1148754/

相关文章:

  • YOLOv12交通标识检测实战:从训练到部署全流程
  • MySQL 8.0的自增主键持久化特性
  • 网易云热门乐评 API 调用限制与用量边界详解
  • MATLAB自适应滤波实战包:LMS基础版+AdaGrad/RMSProp/Adam四种学习率策略一键对比
  • Cursor AI助手试用限制的工程化解决方案:从技术架构到生产部署
  • PPO 算法 PyTorch 2.1 实战:CartPole-v1 环境 500 步稳定训练 3 大调参技巧
  • 马力:汽车动力到底强不强,真的只看它吗?
  • 纯NumPy实现的PCA系异常检测工具集:重建误差、核PCA、鲁棒分解等5种方法
  • XUnity自动翻译器:终极Unity游戏汉化解决方案指南
  • SDF技术实现卡通游戏云层动态消散效果全解析
  • Matlab图形界面版2ASK通信仿真工具:一键运行调制解调+误码率对比分析
  • 光伏MPPT扰动观察法Simulink仿真模型包(含参数配置脚本与原理PDF)
  • VMware Unlocker 4.2.7 实战指南:在Windows/Linux系统上高效运行macOS虚拟机的全面配置攻略
  • 多机器人通信拓扑设计:决定协同性能的底层关键
  • Unity连接API接口:从基础到实战
  • 基于Prometheus与Grafana构建外卖霸王站CPS系统的实时指标监控大盘
  • 关于CP2112的IIC上位机HidSmbus Example的一些资料
  • A3908+PIC18F87J11高精度运动控制系统设计
  • 2624张EL图像标准化基准:太阳能电池缺陷检测的终极开源数据集
  • GitLab CI vs GitHub Actions:面向K8s部署场景的CI/CD工具深度对比与选型决策
  • 终极免费光学仿真指南:5分钟掌握专业级2D光学设计
  • CDMA码分多址系统MATLAB仿真包:含可运行模型、完整源码与实操视频
  • 目前最流行的15个机器学习框架,你知道几个?
  • 二分查找解力扣1011最优运载能力
  • ADP5350与STM32F205RB嵌入式电源管理方案解析
  • ADP5350与STM32F215ZG电源管理方案设计与优化
  • 3分钟快速解密QQ聊天记录数据库:全平台密钥提取终极指南
  • 25个Obsidian终极模板:快速构建你的个人知识管理系统
  • 3秒定位:当手机号成为你的地理侦探,工作效率提升80%的秘密武器
  • 5秒破解百度网盘提取码:开源智能工具终极使用指南