1、环境
Java: 21
Spring Boot: 3.3.4
Maven: 3.9.9
2、实操
2.1 引入依赖
<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-openapi3-jakarta-spring-boot-starter -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId><version>4.5.0</version>
</dependency>
2.2 添加配置文件
package com.jie.springbootinit.config;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.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Map;/*** Knife4j配置* * @author jie*/
@Configuration
public class SwaggerConfig {/*** API标题*/private static final String API_TITLE = "Jie的API";/*** API版本*/private static final String API_VERSION = "1.0";/*** API描述*/private static final String API_DESCRIPTION = "Knife4j集成springdoc-openapi示例";/*** 服务条款URL*/private static final String TERMS_OF_SERVICE_URL = "http://doc.xiaominfo.com";/*** 许可证名称*/private static final String LICENSE_NAME = "Apache 2.0";/*** 许可证URL*/private static final String LICENSE_URL = "http://doc.xiaominfo.com";/*** 根据@Tag 上的排序,写入x-order** @return the global open api customizer*/@Beanpublic GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {return openApi -> {if (openApi.getTags() != null) {openApi.getTags().forEach(tag -> {// 使用Map.of简化Map的创建tag.setExtensions(Map.of("x-order", 1));});}if (openApi.getPaths() != null) {openApi.addExtension("x-test123", "333");openApi.getPaths().addExtension("x-abb", 1);}};}@Beanpublic OpenAPI createCustomOpenApi() { // 修改方法名为createCustomOpenApireturn new OpenAPI().info(new Info().title(API_TITLE).version(API_VERSION).description(API_DESCRIPTION).termsOfService(TERMS_OF_SERVICE_URL).license(new License().name(LICENSE_NAME).url(LICENSE_URL)));}
}
2.3 添加测试的Controller
package com.jie.springbootinit.controller;import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 测试接口* * @author jie*/
@RestController
@RequestMapping("/test")
@Tag(name = "测试接口")
public class TestContrller {@GetMapping("/hello")@Operation(summary = "测试接口")public String hello() {return "Hello, World!";}
}
3、启动项目,查看效果
访问地址:http://localhost:项目端口/doc.html#/home
发送请求
4、Swagger V3(OpenAPI 3)常用注解
注解 | 说明 | 示例 | 使用位置 |
---|---|---|---|
@OpenAPIDefinition | 用于定义整个API的元数据,如信息、服务器等 | @OpenAPIDefinition(info = @Info(title = "API Title", version = "1.0")) | 类级别(通常是主应用类) |
@Info | 定义API的基础信息,如标题、版本、描述等 | @Info(title = "API Title", version = "1.0", description = "API Description") | 作为@OpenAPIDefinition 的属性 |
@Server | 指定API服务器的基本信息,包括URL和描述 | @Server(url = "http://localhost:8080", description = "Local server") | 类级别或方法级别 |
@Tag | 用于为API分组和标记 | @Tag(name = "User Operations", description = "Operations related to users") | 类级别或方法级别 |
@Operation | 用于描述API操作(即每个HTTP请求) | @Operation(summary = "Get user by ID", description = "Returns a user by their ID") | 方法级别 |
@Parameter | 描述操作中的参数 | @Parameter(name = "id", description = "User ID", required = true) | 方法级别的参数 |
@RequestBody | 描述API的请求体 | @RequestBody(description = "User object that needs to be added", required = true) | 方法级别 |
@ApiResponse | 用于定义每个操作的响应信息 | @ApiResponse(responseCode = "200", description = "Successful operation") | 方法级别 |
@Schema | 定义模型对象的属性和元数据 | @Schema(description = "User model", required = true) | 类级别、字段级别 |
@ArraySchema | 用于定义数组类型的Schema | @ArraySchema(schema = @Schema(implementation = User.class)) | 方法级别、字段级别 |
@Content | 用于指定响应和请求体的媒体类型及内容 | @Content(mediaType = "application/json", schema = @Schema(implementation = User.class)) | 作为@RequestBody 或@ApiResponse 的属性 |
@ExampleObject | 用于定义请求或响应的示例 | @ExampleObject(name = "Example 1", value = "{\"id\": 1, \"name\": \"John\"}") | 作为@Content 的属性 |
@Components | 定义API中的可重用组件,如schemas、参数、响应等 | @Components(schemas = @Schema(name = "User", ...)) | 类级别(通常是主应用类) |
@SecurityRequirement | 指定操作所需的安全方案 | @SecurityRequirement(name = "bearerAuth") | 方法级别或类级别 |
@Header | 定义操作中使用的响应头信息 | @Header(name = "X-Rate-Limit", description = "Request rate limit") | 作为@ApiResponse 的属性 |