
你好呀我的老朋友我是老寇跟我一起学习集成Scalr API文档众所周知Knife4j是一个集Swagger2和OpenAPI3为一体的增强解决方案但是社区不太活跃再加上SpringBoot4发版迫切需要一个集成OpenAPI 3.0和OpenAPI 3.1规范的增强解决方案所以才集成ScalarReact集成ScalarReact集成Scalar文档地址安装依赖pnpminstallscalar/api-reference-react配置详情配置项类型是否必填说明示例sourcesArray是OpenAPI 文档源列表一个配置可以配置多个 API 文档。[{ title: 认证授权, url: /api/v3/api-docs }]sources.titleString是API 文档名称在 Scalar 左侧导航中显示。认证授权sources.urlString是OpenAPISwaggerJSON 文档地址。/api-proxy/auth/api/v3/api-docssources.defaultBoolean否是否默认选中该文档。trueproxyUrlString否Scalar 的代理地址用于 Try It、鉴权等请求代理。/api-proxy/auth/scalarexpandAllResponsesBoolean否是否默认展开所有 Response。truehideClientButtonBoolean否是否隐藏 Generate Client生成客户端按钮。trueorderRequiredPropertiesFirstBoolean否Model 属性中是否将必填字段排在前面。trueexpandAllModelSectionsBoolean否是否默认展开所有 Model 定义。false具体代码apiDoc.tsximport{ApiReferenceReact}fromscalar/api-reference-reactimportscalar/api-reference-react/style.cssimport{useIntl}from/exports;exportdefault(){constintluseIntl();constt(id:string,values?:Recordstring,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 groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-gateway-server-webflux/artifactId /dependency具体代码ScalarGatewayFilterFactorySlf4jComponentpublicclassScalarGatewayFilterFactoryextendsAbstractGatewayFilterFactoryScalarGatewayFilterFactory.NonNullConfigimplementsOrdered{publicScalarGatewayFilterFactory(){super(Config.class);}OverridepublicintgetOrder(){returnOrdered.LOWEST_PRECEDENCE-2000;}NonNullOverridepublicGatewayFilterapply(Configconfig){return(exchange,chain)-{ServerHttpRequestrequestexchange.getRequest();// 获取uriStringrequestURLReactiveRequestUtils.getRequestURL(request);// scalar重写地址if(ReactiveRequestUtils.pathMatcher(/**/scalar,requestURL)){StringscalarUrlReactiveRequestUtils.getParamValue(request,scalar_url);URIuriURI.create(scalarUrl);returnchain.filter(exchange.mutate().request(request.mutate().uri(uri).path(uri.getPath()).build()).build());}returnchain.filter(exchange);};}publicstaticclassConfig{}}application.ymlspring: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:ScalarSpringBoot集成Scalar以laokou-auth为例安装依赖dependencygroupIdorg.springdoc/groupIdartifactIdspringdoc-openapi-starter-webmvc-scalar/artifactId/dependency具体代码OpenApiDocConfigConfigurationpublicclassOpenApiDocConfig{BeanOpenAPIopenApi(){returnnewOpenAPI().info(newInfo().title(API文档).description(API文档).version(1.0.0).contact(newContact().name(laokou).url(https://github.com/KouShenhai).email(2413176044qq.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.ymlscalar:enabled:truespringdoc:swagger-ui:enabled:falseapi-docs:enabled:truepath:/v3/api-docsversion:openapi_3_1我是老寇我们下次再见啦