springcloud springboot3.3.4 集成 knife4j-openapi3-jakarta-spring-boot-starter
·
gateway配置
依赖
<!--网关聚合接口文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
<version>4.5.0</version>
</dependency>
配置:
bootstrap.yml
knife4j:
gateway:
basic:
username: xxx
password: xxx
enable: true
enabled: true
# 指定服务发现的模式聚合微服务文档
strategy: discover
discover:
enabled: true
# 指定版本号(Swagger2|OpenAPI3)
version : openapi3
# 需要排除的微服务(eg:网关服务)
excluded-services:
- gateway-service
spring:
application:
# 应用名称
name: gateway-service
main:
web-application-type: reactive
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: order-service
uri: lb://order-service
predicates:
- Path=/gateway/order/**
filters:
- StripPrefix=1
filters:
- StripPrefix=1
业务模块配置
依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.5.0</version>
</dependency>
配置:
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Knife4jConfig {
@Bean
public OpenAPI customOpenApi() {
return new OpenAPI()
.info(new Info()
.title("后台服务接口")
.version("1.0")
.description( "Knife4j集成springdoc-openapi示例")
.termsOfService("http://doc.xiaominfo.com")
.license(new License().name("Apache 2.0")
.url("http://fadsfas.xiaominfo.com")));
}
}
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
#operations-sorter: order
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
display-name: '后台页面接口'
paths-to-match: '/**'
packages-to-scan: com.xxx.xxx.xxx.controller
default-flat-param-object: true
knife4j:
enable: true
basic:
username: xxx
password: xxx
setting:
language: zh_cn
注解 描述
@OpenAPIDefinition 定义 OpenAPI 规范的基本信息,如 API 的标题、版本、服务器等
@Operation 用于描述 API 操作(端点),包括操作的摘要、描述、请求和响应等信息
@ApiResponse 定义操作的响应,包括响应的状态码、描述和响应模型等信息
@Parameter 定义操作的参数,包括路径参数、查询参数、请求头参数等
@PathVariable 定义路径参数,用于提取 URL 中的变量
@RequestParam 定义查询参数,用于从请求中获取参数的值
@ApiParam 在方法参数上使用,用于描述参数的含义和约束
@ApiResponses 在控制器类上使用,为多个操作定义通用的响应规范
@ApiResponseExtension 在 @Operation 或 @ApiResponse 上使用,用于扩展响应信息
@SecurityRequirement 定义操作所需的安全要求,如需要的身份验证方案、安全范围等
@SecurityScheme 定义安全方案,包括认证方式(如 Basic、OAuth2 等)、令牌 URL、授权 URL 等
@Tags 定义操作的标签,用于对操作进行分类和组织
@Hidden 在文档中隐藏标记的操作或参数,可以用于隐藏一些内部或不需要在文档中展示的部分
@Extension 用于为生成的 OpenAPI 文档添加自定义的扩展信息,可以在文档中增加额外的元数据或自定义字段
@RequestBodySchema 定义请求体的数据模型,允许对请求体进行更细粒度的描述和约束,如属性的名称、类型、格式、必填性等
@ApiResponseSchema 定义响应的数据模型,允许对响应体进行更细粒度的描述和约束,如属性的名称、类型、格式、必填性等
@ExtensionProperty 在 @Extension 注解上使用,用于定义自定义扩展的属性,可以添加额外的元数据或自定义字段到生成的 OpenAPI 文档中
Springfox改用Springdoc后,注解改变:
@Api → @Tag
@ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
@ApiImplicitParam → @Parameter
@ApiImplicitParams → @Parameters
@ApiModel → @Schema
@ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
@ApiModelProperty → @Schema
@ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
@ApiParam → @Parameter
@ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")
启动后访问 网关 localhost:xxx/doc.html
更多推荐


所有评论(0)