活动公告

系统通知
07-14 23:24
系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

深入浅出Swagger注解示例代码从入门到精通的完整指南涵盖常用注解详解实际应用场景助你快速提升API开发能力

SunJu_FaceMall

3万

主题

3056

科技点

3万

积分

执行版主

碾压王

积分
32876

塔罗立华奏

执行版主 发表于 2025-10-1 23:40:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

Swagger是一个强大且广泛使用的API开发工具集,它使得设计和文档化RESTful API变得更加简单。通过Swagger注解,开发者可以直接在代码中描述API,从而自动生成交互式API文档,提高开发效率和团队协作能力。本文将深入浅出地介绍Swagger注解的使用,从基础概念到高级应用,帮助你全面掌握Swagger,提升API开发能力。

Swagger基础知识

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码中,允许API始终保持同步。

在Java项目中,我们通常使用Springfox或SpringDoc来实现Swagger集成。Springfox适用于较早的Spring Boot版本,而SpringDoc则支持Spring Boot 2.x和3.x以及OpenAPI 3规范。本文将以SpringDoc为例进行讲解,因为它代表了未来的发展方向。

首先,我们需要在项目中添加Swagger依赖:
  1. <!-- 对于Maven项目 -->
  2. <dependency>
  3.     <groupId>org.springdoc</groupId>
  4.     <artifactId>springdoc-openapi-ui</artifactId>
  5.     <version>1.6.14</version>
  6. </dependency>
复制代码

或者对于Gradle项目:
  1. implementation 'org.springdoc:springdoc-openapi-ui:1.6.14'
复制代码

添加依赖后,我们可以通过访问http://localhost:8080/swagger-ui.html来查看Swagger UI界面。

Swagger常用注解详解

Swagger提供了丰富的注解来描述API。这些注解可以分为类级别、方法级别、参数级别和模型级别。下面我们详细介绍这些注解及其用法。

类级别注解

@Tag注解用于在控制器类上,为API分组提供名称和描述。
  1. import io.swagger.v3.oas.annotations.tags.Tag;
  2. @Tag(name = "用户管理", description = "用户相关的API操作")
  3. @RestController
  4. @RequestMapping("/api/users")
  5. public class UserController {
  6.     // 控制器方法
  7. }
复制代码

在Swagger UI中,这个控制器下的所有API都会被归类到”用户管理”标签下,并显示描述信息。

@SecurityScheme注解用于定义安全方案,如API密钥、OAuth2等。
  1. import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
  2. import io.swagger.v3.oas.annotations.security.SecurityScheme;
  3. @SecurityScheme(
  4.     name = "bearerAuth",
  5.     type = SecuritySchemeType.HTTP,
  6.     bearerFormat = "JWT",
  7.     scheme = "bearer"
  8. )
  9. @SpringBootApplication
  10. public class MyApp {
  11.     public static void main(String[] args) {
  12.         SpringApplication.run(MyApp.class, args);
  13.     }
  14. }
复制代码

这个配置定义了一个名为”bearerAuth”的JWT认证方案。

方法级别注解

@Operation注解用于描述单个API操作,包括摘要、详细描述等。
  1. import io.swagger.v3.oas.annotations.Operation;
  2. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  3. import io.swagger.v3.oas.annotations.responses.ApiResponses;
  4. @Operation(
  5.     summary = "获取用户信息",
  6.     description = "根据用户ID获取用户的详细信息",
  7.     tags = { "用户管理" }
  8. )
  9. @ApiResponses(value = {
  10.     @ApiResponse(responseCode = "200", description = "成功获取用户信息"),
  11.     @ApiResponse(responseCode = "404", description = "用户不存在"),
  12.     @ApiResponse(responseCode = "500", description = "服务器内部错误")
  13. })
  14. @GetMapping("/{id}")
  15. public ResponseEntity<User> getUserById(@PathVariable Long id) {
  16.     // 方法实现
  17. }
复制代码

@ApiResponse注解用于描述API可能的响应。通常与@ApiResponses一起使用,可以定义多个响应。
  1. @ApiResponses(value = {
  2.     @ApiResponse(responseCode = "200", description = "成功获取用户信息",
  3.         content = { @Content(mediaType = "application/json",
  4.             schema = @Schema(implementation = User.class)) }),
  5.     @ApiResponse(responseCode = "404", description = "用户不存在",
  6.         content = @Content),
  7.     @ApiResponse(responseCode = "500", description = "服务器内部错误",
  8.         content = @Content)
  9. })
  10. @GetMapping("/{id}")
  11. public ResponseEntity<User> getUserById(@PathVariable Long id) {
  12.     // 方法实现
  13. }
复制代码

@Parameters注解用于描述API的所有参数。当有多个参数需要描述时,可以使用此注解。
  1. import io.swagger.v3.oas.annotations.Parameter;
  2. import io.swagger.v3.oas.annotations.Parameters;
  3. @Parameters({
  4.     @Parameter(name = "id", description = "用户ID", required = true, example = "1"),
  5.     @Parameter(name = "fields", description = "需要返回的字段,多个字段用逗号分隔",
  6.                required = false, example = "id,name,email")
  7. })
  8. @GetMapping("/{id}")
  9. public ResponseEntity<User> getUserById(
  10.     @PathVariable Long id,
  11.     @RequestParam(required = false) String fields) {
  12.     // 方法实现
  13. }
复制代码

参数级别注解

@Parameter注解用于描述方法参数,包括名称、描述、是否必需等。
  1. @GetMapping("/search")
  2. public ResponseEntity<List<User>> searchUsers(
  3.     @Parameter(description = "搜索关键词", required = true, example = "john")
  4.     @RequestParam String keyword,
  5.    
  6.     @Parameter(description = "页码", required = false, example = "1")
  7.     @RequestParam(defaultValue = "1") int page,
  8.    
  9.     @Parameter(description = "每页大小", required = false, example = "10")
  10.     @RequestParam(defaultValue = "10") int size) {
  11.     // 方法实现
  12. }
复制代码

@RequestBody注解与@Content和@Schema结合使用,用于描述请求体。
  1. import io.swagger.v3.oas.annotations.media.Content;
  2. import io.swagger.v3.oas.annotations.media.Schema;
  3. import io.swagger.v3.oas.annotations.parameters.RequestBody;
  4. @Operation(summary = "创建用户", description = "创建一个新的用户")
  5. @PostMapping("/")
  6. public ResponseEntity<User> createUser(
  7.     @RequestBody(description = "用户信息", required = true,
  8.         content = @Content(
  9.             mediaType = "application/json",
  10.             schema = @Schema(implementation = UserCreateRequest.class)
  11.         )
  12.     )
  13.     @Valid @RequestBody UserCreateRequest userRequest) {
  14.     // 方法实现
  15. }
复制代码

模型注解

@Schema注解用于描述模型类及其属性。
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. @Schema(description = "用户信息")
  3. public class User {
  4.    
  5.     @Schema(description = "用户ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
  6.     private Long id;
  7.    
  8.     @Schema(description = "用户名", example = "john_doe", requiredMode = Schema.RequiredMode.REQUIRED)
  9.     private String username;
  10.    
  11.     @Schema(description = "电子邮箱", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.REQUIRED)
  12.     private String email;
  13.    
  14.     @Schema(description = "创建时间", example = "2023-01-01T12:00:00Z", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  15.     private LocalDateTime createdAt;
  16.    
  17.     // getters and setters
  18. }
复制代码

@ArraySchema注解用于描述数组类型的模型。
  1. @ArraySchema(schema = @Schema(implementation = User.class))
  2. private List<User> users;
复制代码

实际应用场景与示例

场景一:构建用户管理API

让我们通过一个完整的用户管理API示例,展示如何在实际项目中应用Swagger注解。

首先,定义用户模型:
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. import java.time.LocalDateTime;
  3. @Schema(description = "用户信息")
  4. public class User {
  5.    
  6.     @Schema(description = "用户ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
  7.     private Long id;
  8.    
  9.     @Schema(description = "用户名", example = "john_doe", requiredMode = Schema.RequiredMode.REQUIRED)
  10.     private String username;
  11.    
  12.     @Schema(description = "电子邮箱", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.REQUIRED)
  13.     private String email;
  14.    
  15.     @Schema(description = "手机号码", example = "13800138000", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  16.     private String phone;
  17.    
  18.     @Schema(description = "用户状态", example = "ACTIVE", requiredMode = Schema.RequiredMode.REQUIRED)
  19.     private UserStatus status;
  20.    
  21.     @Schema(description = "创建时间", example = "2023-01-01T12:00:00Z", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  22.     private LocalDateTime createdAt;
  23.    
  24.     @Schema(description = "更新时间", example = "2023-01-01T12:00:00Z", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  25.     private LocalDateTime updatedAt;
  26.    
  27.     // 枚举定义用户状态
  28.     public enum UserStatus {
  29.         ACTIVE, INACTIVE, DISABLED
  30.     }
  31.    
  32.     // getters and setters
  33. }
复制代码

然后,定义创建用户的请求DTO:
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. import javax.validation.constraints.Email;
  3. import javax.validation.constraints.NotBlank;
  4. import javax.validation.constraints.Pattern;
  5. import javax.validation.constraints.Size;
  6. @Schema(description = "创建用户请求")
  7. public class UserCreateRequest {
  8.    
  9.     @NotBlank(message = "用户名不能为空")
  10.     @Size(min = 3, max = 20, message = "用户名长度必须在3到20个字符之间")
  11.     @Schema(description = "用户名", example = "john_doe", requiredMode = Schema.RequiredMode.REQUIRED)
  12.     private String username;
  13.    
  14.     @NotBlank(message = "电子邮箱不能为空")
  15.     @Email(message = "电子邮箱格式不正确")
  16.     @Schema(description = "电子邮箱", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.REQUIRED)
  17.     private String email;
  18.    
  19.     @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确")
  20.     @Schema(description = "手机号码", example = "13800138000", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  21.     private String phone;
  22.    
  23.     @Schema(description = "密码", example = "Password123!", requiredMode = Schema.RequiredMode.REQUIRED)
  24.     private String password;
  25.    
  26.     // getters and setters
  27. }
复制代码

定义更新用户的请求DTO:
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. @Schema(description = "更新用户请求")
  3. public class UserUpdateRequest {
  4.    
  5.     @Schema(description = "电子邮箱", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  6.     private String email;
  7.    
  8.     @Schema(description = "手机号码", example = "13800138000", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  9.     private String phone;
  10.    
  11.     @Schema(description = "用户状态", example = "ACTIVE", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  12.     private User.UserStatus status;
  13.    
  14.     // getters and setters
  15. }
复制代码

现在,创建用户控制器:
  1. import io.swagger.v3.oas.annotations.Operation;
  2. import io.swagger.v3.oas.annotations.Parameter;
  3. import io.swagger.v3.oas.annotations.media.Content;
  4. import io.swagger.v3.oas.annotations.media.Schema;
  5. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  6. import io.swagger.v3.oas.annotations.responses.ApiResponses;
  7. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.*;
  11. import javax.validation.Valid;
  12. import java.util.List;
  13. @Tag(name = "用户管理", description = "用户相关的API操作")
  14. @RestController
  15. @RequestMapping("/api/users")
  16. @SecurityRequirement(name = "bearerAuth")
  17. public class UserController {
  18.     private final UserService userService;
  19.     public UserController(UserService userService) {
  20.         this.userService = userService;
  21.     }
  22.     @Operation(
  23.         summary = "创建用户",
  24.         description = "创建一个新的用户账户"
  25.     )
  26.     @ApiResponses(value = {
  27.         @ApiResponse(responseCode = "201", description = "用户创建成功",
  28.             content = { @Content(mediaType = "application/json",
  29.                 schema = @Schema(implementation = User.class)) }),
  30.         @ApiResponse(responseCode = "400", description = "请求参数不正确",
  31.             content = @Content),
  32.         @ApiResponse(responseCode = "409", description = "用户名或邮箱已存在",
  33.             content = @Content)
  34.     })
  35.     @PostMapping("/")
  36.     public ResponseEntity<User> createUser(
  37.         @Parameter(description = "用户信息", required = true)
  38.         @Valid @RequestBody UserCreateRequest userRequest) {
  39.         User createdUser = userService.createUser(userRequest);
  40.         return ResponseEntity.status(201).body(createdUser);
  41.     }
  42.     @Operation(
  43.         summary = "获取用户",
  44.         description = "根据用户ID获取用户详细信息"
  45.     )
  46.     @ApiResponses(value = {
  47.         @ApiResponse(responseCode = "200", description = "成功获取用户信息",
  48.             content = { @Content(mediaType = "application/json",
  49.                 schema = @Schema(implementation = User.class)) }),
  50.         @ApiResponse(responseCode = "404", description = "用户不存在",
  51.             content = @Content)
  52.     })
  53.     @GetMapping("/{id}")
  54.     public ResponseEntity<User> getUserById(
  55.         @Parameter(description = "用户ID", required = true, example = "1")
  56.         @PathVariable Long id) {
  57.         User user = userService.getUserById(id);
  58.         return ResponseEntity.ok(user);
  59.     }
  60.     @Operation(
  61.         summary = "更新用户",
  62.         description = "根据用户ID更新用户信息"
  63.     )
  64.     @ApiResponses(value = {
  65.         @ApiResponse(responseCode = "200", description = "用户更新成功",
  66.             content = { @Content(mediaType = "application/json",
  67.                 schema = @Schema(implementation = User.class)) }),
  68.         @ApiResponse(responseCode = "400", description = "请求参数不正确",
  69.             content = @Content),
  70.         @ApiResponse(responseCode = "404", description = "用户不存在",
  71.             content = @Content)
  72.     })
  73.     @PutMapping("/{id}")
  74.     public ResponseEntity<User> updateUser(
  75.         @Parameter(description = "用户ID", required = true, example = "1")
  76.         @PathVariable Long id,
  77.         @Parameter(description = "更新信息", required = true)
  78.         @Valid @RequestBody UserUpdateRequest userRequest) {
  79.         User updatedUser = userService.updateUser(id, userRequest);
  80.         return ResponseEntity.ok(updatedUser);
  81.     }
  82.     @Operation(
  83.         summary = "删除用户",
  84.         description = "根据用户ID删除用户"
  85.     )
  86.     @ApiResponses(value = {
  87.         @ApiResponse(responseCode = "204", description = "用户删除成功",
  88.             content = @Content),
  89.         @ApiResponse(responseCode = "404", description = "用户不存在",
  90.             content = @Content)
  91.     })
  92.     @DeleteMapping("/{id}")
  93.     public ResponseEntity<Void> deleteUser(
  94.         @Parameter(description = "用户ID", required = true, example = "1")
  95.         @PathVariable Long id) {
  96.         userService.deleteUser(id);
  97.         return ResponseEntity.noContent().build();
  98.     }
  99.     @Operation(
  100.         summary = "获取用户列表",
  101.         description = "分页获取用户列表,支持按关键词搜索"
  102.     )
  103.     @ApiResponses(value = {
  104.         @ApiResponse(responseCode = "200", description = "成功获取用户列表",
  105.             content = { @Content(mediaType = "application/json",
  106.                 schema = @Schema(implementation = PageResult.class)) })
  107.     })
  108.     @GetMapping("/")
  109.     public ResponseEntity<PageResult<User>> getUsers(
  110.         @Parameter(description = "页码", required = false, example = "1")
  111.         @RequestParam(defaultValue = "1") int page,
  112.         
  113.         @Parameter(description = "每页大小", required = false, example = "10")
  114.         @RequestParam(defaultValue = "10") int size,
  115.         
  116.         @Parameter(description = "搜索关键词", required = false, example = "john")
  117.         @RequestParam(required = false) String keyword,
  118.         
  119.         @Parameter(description = "用户状态过滤", required = false, example = "ACTIVE")
  120.         @RequestParam(required = false) User.UserStatus status) {
  121.         
  122.         PageResult<User> users = userService.getUsers(page, size, keyword, status);
  123.         return ResponseEntity.ok(users);
  124.     }
  125. }
复制代码

最后,定义分页结果模型:
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. import java.util.List;
  3. @Schema(description = "分页结果")
  4. public class PageResult<T> {
  5.    
  6.     @Schema(description = "数据列表", requiredMode = Schema.RequiredMode.REQUIRED)
  7.     private List<T> content;
  8.    
  9.     @Schema(description = "当前页码", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
  10.     private int pageNumber;
  11.    
  12.     @Schema(description = "每页大小", example = "10", requiredMode = Schema.RequiredMode.REQUIRED)
  13.     private int pageSize;
  14.    
  15.     @Schema(description = "总记录数", example = "100", requiredMode = Schema.RequiredMode.REQUIRED)
  16.     private long totalElements;
  17.    
  18.     @Schema(description = "总页数", example = "10", requiredMode = Schema.RequiredMode.REQUIRED)
  19.     private int totalPages;
  20.    
  21.     @Schema(description = "是否有下一页", example = "true", requiredMode = Schema.RequiredMode.REQUIRED)
  22.     private boolean hasNext;
  23.    
  24.     @Schema(description = "是否有上一页", example = "false", requiredMode = Schema.RequiredMode.REQUIRED)
  25.     private boolean hasPrevious;
  26.    
  27.     // getters and setters
  28. }
复制代码

场景二:构建文件上传API

文件上传是Web应用中常见的功能,下面展示如何使用Swagger注解描述文件上传API。
  1. import io.swagger.v3.oas.annotations.Operation;
  2. import io.swagger.v3.oas.annotations.Parameter;
  3. import io.swagger.v3.oas.annotations.media.Content;
  4. import io.swagger.v3.oas.annotations.media.Schema;
  5. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.*;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. @Tag(name = "文件管理", description = "文件上传和下载相关的API操作")
  14. @RestController
  15. @RequestMapping("/api/files")
  16. @SecurityRequirement(name = "bearerAuth")
  17. public class FileController {
  18.     @Operation(
  19.         summary = "上传文件",
  20.         description = "上传单个文件,支持图片、文档等格式"
  21.     )
  22.     @ApiResponses(value = {
  23.         @ApiResponse(responseCode = "200", description = "文件上传成功",
  24.             content = { @Content(mediaType = "application/json",
  25.                 schema = @Schema(implementation = FileUploadResult.class)) }),
  26.         @ApiResponse(responseCode = "400", description = "请求参数不正确",
  27.             content = @Content)
  28.     })
  29.     @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  30.     public ResponseEntity<FileUploadResult> uploadFile(
  31.         @Parameter(description = "要上传的文件", required = true)
  32.         @RequestParam("file") MultipartFile file,
  33.         
  34.         @Parameter(description = "文件分类", required = false, example = "image")
  35.         @RequestParam(required = false) String category) {
  36.         
  37.         // 模拟文件上传处理
  38.         String fileId = "file_" + System.currentTimeMillis();
  39.         String fileName = file.getOriginalFilename();
  40.         long fileSize = file.getSize();
  41.         String contentType = file.getContentType();
  42.         
  43.         FileUploadResult result = new FileUploadResult(fileId, fileName, fileSize, contentType, category);
  44.         return ResponseEntity.ok(result);
  45.     }
  46.     @Operation(
  47.         summary = "批量上传文件",
  48.         description = "一次上传多个文件"
  49.     )
  50.     @ApiResponses(value = {
  51.         @ApiResponse(responseCode = "200", description = "文件上传成功",
  52.             content = { @Content(mediaType = "application/json",
  53.                 schema = @Schema(implementation = FileUploadResult.class)) }),
  54.         @ApiResponse(responseCode = "400", description = "请求参数不正确",
  55.             content = @Content)
  56.     })
  57.     @PostMapping(value = "/batch-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  58.     public ResponseEntity<List<FileUploadResult>> batchUploadFiles(
  59.         @Parameter(description = "要上传的文件列表", required = true)
  60.         @RequestParam("files") MultipartFile[] files,
  61.         
  62.         @Parameter(description = "文件分类", required = false, example = "image")
  63.         @RequestParam(required = false) String category) {
  64.         
  65.         // 模拟批量文件上传处理
  66.         List<FileUploadResult> results = new ArrayList<>();
  67.         for (MultipartFile file : files) {
  68.             String fileId = "file_" + System.currentTimeMillis();
  69.             String fileName = file.getOriginalFilename();
  70.             long fileSize = file.getSize();
  71.             String contentType = file.getContentType();
  72.             
  73.             results.add(new FileUploadResult(fileId, fileName, fileSize, contentType, category));
  74.         }
  75.         
  76.         return ResponseEntity.ok(results);
  77.     }
  78.     @Operation(
  79.         summary = "下载文件",
  80.         description = "根据文件ID下载文件"
  81.     )
  82.     @ApiResponses(value = {
  83.         @ApiResponse(responseCode = "200", description = "文件下载成功",
  84.             content = { @Content(mediaType = "application/octet-stream") }),
  85.         @ApiResponse(responseCode = "404", description = "文件不存在",
  86.             content = @Content)
  87.     })
  88.     @GetMapping("/download/{fileId}")
  89.     public ResponseEntity<byte[]> downloadFile(
  90.         @Parameter(description = "文件ID", required = true, example = "file_123456789")
  91.         @PathVariable String fileId) {
  92.         
  93.         // 模拟文件下载处理
  94.         byte[] fileContent = "Mock file content".getBytes();
  95.         String filename = "example.txt";
  96.         
  97.         return ResponseEntity.ok()
  98.                 .header("Content-Disposition", "attachment; filename="" + filename + """)
  99.                 .contentType(MediaType.APPLICATION_OCTET_STREAM)
  100.                 .body(fileContent);
  101.     }
  102. }
  103. // 文件上传结果模型
  104. @Schema(description = "文件上传结果")
  105. class FileUploadResult {
  106.    
  107.     @Schema(description = "文件ID", example = "file_123456789", requiredMode = Schema.RequiredMode.REQUIRED)
  108.     private String fileId;
  109.    
  110.     @Schema(description = "文件名", example = "example.jpg", requiredMode = Schema.RequiredMode.REQUIRED)
  111.     private String fileName;
  112.    
  113.     @Schema(description = "文件大小(字节)", example = "10240", requiredMode = Schema.RequiredMode.REQUIRED)
  114.     private long fileSize;
  115.    
  116.     @Schema(description = "文件类型", example = "image/jpeg", requiredMode = Schema.RequiredMode.REQUIRED)
  117.     private String contentType;
  118.    
  119.     @Schema(description = "文件分类", example = "image", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  120.     private String category;
  121.    
  122.     @Schema(description = "文件URL", example = "http://example.com/files/file_123456789", requiredMode = Schema.RequiredMode.REQUIRED)
  123.     private String url;
  124.    
  125.     public FileUploadResult(String fileId, String fileName, long fileSize, String contentType, String category) {
  126.         this.fileId = fileId;
  127.         this.fileName = fileName;
  128.         this.fileSize = fileSize;
  129.         this.contentType = contentType;
  130.         this.category = category;
  131.         this.url = "http://example.com/files/" + fileId;
  132.     }
  133.    
  134.     // getters and setters
  135. }
复制代码

场景三:构建带权限控制的API

在实际应用中,很多API需要权限控制。下面展示如何使用Swagger注解描述带权限控制的API。

首先,定义角色枚举:
  1. import io.swagger.v3.oas.annotations.media.Schema;
  2. @Schema(description = "用户角色")
  3. public enum UserRole {
  4.     @Schema(description = "管理员")
  5.     ADMIN,
  6.    
  7.     @Schema(description = "普通用户")
  8.     USER,
  9.    
  10.     @Schema(description = "访客")
  11.     GUEST
  12. }
复制代码

然后,创建带权限控制的API控制器:
  1. import io.swagger.v3.oas.annotations.Operation;
  2. import io.swagger.v3.oas.annotations.Parameter;
  3. import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
  4. import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
  5. import io.swagger.v3.oas.annotations.media.Content;
  6. import io.swagger.v3.oas.annotations.media.Schema;
  7. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  8. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  9. import io.swagger.v3.oas.annotations.security.SecurityScheme;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.security.access.prepost.PreAuthorize;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. @SecurityScheme(
  17.     name = "bearerAuth",
  18.     type = SecuritySchemeType.HTTP,
  19.     scheme = "bearer",
  20.     bearerFormat = "JWT",
  21.     in = SecuritySchemeIn.HEADER,
  22.     description = "JWT认证"
  23. )
  24. @Tag(name = "系统管理", description = "系统管理相关的API操作")
  25. @RestController
  26. @RequestMapping("/api/admin")
  27. @SecurityRequirement(name = "bearerAuth")
  28. public class AdminController {
  29.     @Operation(
  30.         summary = "获取系统配置",
  31.         description = "获取系统全局配置信息,仅管理员可访问"
  32.     )
  33.     @ApiResponses(value = {
  34.         @ApiResponse(responseCode = "200", description = "成功获取系统配置",
  35.             content = { @Content(mediaType = "application/json",
  36.                 schema = @Schema(implementation = SystemConfig.class)) }),
  37.         @ApiResponse(responseCode = "403", description = "权限不足",
  38.             content = @Content)
  39.     })
  40.     @PreAuthorize("hasRole('ADMIN')")
  41.     @GetMapping("/config")
  42.     public ResponseEntity<SystemConfig> getSystemConfig() {
  43.         // 模拟获取系统配置
  44.         SystemConfig config = new SystemConfig(
  45.             "My Application",
  46.             "1.0.0",
  47.             Arrays.asList("en", "zh", "ja"),
  48.             true,
  49.             100
  50.         );
  51.         return ResponseEntity.ok(config);
  52.     }
  53.     @Operation(
  54.         summary = "更新系统配置",
  55.         description = "更新系统全局配置信息,仅管理员可访问"
  56.     )
  57.     @ApiResponses(value = {
  58.         @ApiResponse(responseCode = "200", description = "成功更新系统配置",
  59.             content = { @Content(mediaType = "application/json",
  60.                 schema = @Schema(implementation = SystemConfig.class)) }),
  61.         @ApiResponse(responseCode = "400", description = "请求参数不正确",
  62.             content = @Content),
  63.         @ApiResponse(responseCode = "403", description = "权限不足",
  64.             content = @Content)
  65.     })
  66.     @PreAuthorize("hasRole('ADMIN')")
  67.     @PutMapping("/config")
  68.     public ResponseEntity<SystemConfig> updateSystemConfig(
  69.         @Parameter(description = "系统配置信息", required = true)
  70.         @Valid @RequestBody SystemConfigUpdateRequest configRequest) {
  71.         
  72.         // 模拟更新系统配置
  73.         SystemConfig updatedConfig = new SystemConfig(
  74.             configRequest.getAppName(),
  75.             configRequest.getVersion(),
  76.             configRequest.getSupportedLanguages(),
  77.             configRequest.isMaintenanceMode(),
  78.             configRequest.getMaxUploadSize()
  79.         );
  80.         return ResponseEntity.ok(updatedConfig);
  81.     }
  82.     @Operation(
  83.         summary = "获取用户列表",
  84.         description = "分页获取系统所有用户,管理员可访问"
  85.     )
  86.     @ApiResponses(value = {
  87.         @ApiResponse(responseCode = "200", description = "成功获取用户列表",
  88.             content = { @Content(mediaType = "application/json",
  89.                 schema = @Schema(implementation = PageResult.class)) }),
  90.         @ApiResponse(responseCode = "403", description = "权限不足",
  91.             content = @Content)
  92.     })
  93.     @PreAuthorize("hasRole('ADMIN')")
  94.     @GetMapping("/users")
  95.     public ResponseEntity<PageResult<User>> getUsers(
  96.         @Parameter(description = "页码", required = false, example = "1")
  97.         @RequestParam(defaultValue = "1") int page,
  98.         
  99.         @Parameter(description = "每页大小", required = false, example = "10")
  100.         @RequestParam(defaultValue = "10") int size,
  101.         
  102.         @Parameter(description = "角色过滤", required = false, example = "USER")
  103.         @RequestParam(required = false) UserRole role) {
  104.         
  105.         // 模拟获取用户列表
  106.         List<User> users = Arrays.asList(
  107.             new User(1L, "admin", "admin@example.com", UserRole.ADMIN),
  108.             new User(2L, "user1", "user1@example.com", UserRole.USER),
  109.             new User(3L, "user2", "user2@example.com", UserRole.USER)
  110.         );
  111.         
  112.         PageResult<User> result = new PageResult<>(
  113.             users,
  114.             page,
  115.             size,
  116.             users.size(),
  117.             (int) Math.ceil((double) users.size() / size),
  118.             page < Math.ceil((double) users.size() / size),
  119.             page > 1
  120.         );
  121.         
  122.         return ResponseEntity.ok(result);
  123.     }
  124.     @Operation(
  125.         summary = "更新用户角色",
  126.         description = "更新指定用户的角色,仅管理员可访问"
  127.     )
  128.     @ApiResponses(value = {
  129.         @ApiResponse(responseCode = "200", description = "成功更新用户角色",
  130.             content = { @Content(mediaType = "application/json",
  131.                 schema = @Schema(implementation = User.class)) }),
  132.         @ApiResponse(responseCode = "404", description = "用户不存在",
  133.             content = @Content),
  134.         @ApiResponse(responseCode = "403", description = "权限不足",
  135.             content = @Content)
  136.     })
  137.     @PreAuthorize("hasRole('ADMIN')")
  138.     @PutMapping("/users/{userId}/role")
  139.     public ResponseEntity<User> updateUserRole(
  140.         @Parameter(description = "用户ID", required = true, example = "1")
  141.         @PathVariable Long userId,
  142.         
  143.         @Parameter(description = "用户角色", required = true, example = "ADMIN")
  144.         @RequestParam UserRole role) {
  145.         
  146.         // 模拟更新用户角色
  147.         User updatedUser = new User(userId, "user", "user@example.com", role);
  148.         return ResponseEntity.ok(updatedUser);
  149.     }
  150. }
  151. // 系统配置模型
  152. @Schema(description = "系统配置")
  153. class SystemConfig {
  154.    
  155.     @Schema(description = "应用名称", example = "My Application", requiredMode = Schema.RequiredMode.REQUIRED)
  156.     private String appName;
  157.    
  158.     @Schema(description = "应用版本", example = "1.0.0", requiredMode = Schema.RequiredMode.REQUIRED)
  159.     private String version;
  160.    
  161.     @Schema(description = "支持的语言列表", requiredMode = Schema.RequiredMode.REQUIRED)
  162.     private List<String> supportedLanguages;
  163.    
  164.     @Schema(description = "是否处于维护模式", example = "false", requiredMode = Schema.RequiredMode.REQUIRED)
  165.     private boolean maintenanceMode;
  166.    
  167.     @Schema(description = "最大上传大小(MB)", example = "100", requiredMode = Schema.RequiredMode.REQUIRED)
  168.     private int maxUploadSize;
  169.    
  170.     public SystemConfig(String appName, String version, List<String> supportedLanguages,
  171.                        boolean maintenanceMode, int maxUploadSize) {
  172.         this.appName = appName;
  173.         this.version = version;
  174.         this.supportedLanguages = supportedLanguages;
  175.         this.maintenanceMode = maintenanceMode;
  176.         this.maxUploadSize = maxUploadSize;
  177.     }
  178.    
  179.     // getters and setters
  180. }
  181. // 系统配置更新请求模型
  182. @Schema(description = "系统配置更新请求")
  183. class SystemConfigUpdateRequest {
  184.    
  185.     @Schema(description = "应用名称", example = "My Application", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  186.     private String appName;
  187.    
  188.     @Schema(description = "应用版本", example = "1.0.0", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  189.     private String version;
  190.    
  191.     @Schema(description = "支持的语言列表", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  192.     private List<String> supportedLanguages;
  193.    
  194.     @Schema(description = "是否处于维护模式", example = "false", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  195.     private Boolean maintenanceMode;
  196.    
  197.     @Schema(description = "最大上传大小(MB)", example = "100", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  198.     private Integer maxUploadSize;
  199.    
  200.     // getters and setters
  201. }
  202. // 用户模型(简化版)
  203. @Schema(description = "用户信息")
  204. class User {
  205.    
  206.     @Schema(description = "用户ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
  207.     private Long id;
  208.    
  209.     @Schema(description = "用户名", example = "john_doe", requiredMode = Schema.RequiredMode.REQUIRED)
  210.     private String username;
  211.    
  212.     @Schema(description = "电子邮箱", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.REQUIRED)
  213.     private String email;
  214.    
  215.     @Schema(description = "用户角色", example = "USER", requiredMode = Schema.RequiredMode.REQUIRED)
  216.     private UserRole role;
  217.    
  218.     public User(Long id, String username, String email, UserRole role) {
  219.         this.id = id;
  220.         this.username = username;
  221.         this.email = email;
  222.         this.role = role;
  223.     }
  224.    
  225.     // getters and setters
  226. }
复制代码

高级技巧与最佳实践

1. 分组管理API

在大型项目中,API数量可能很多,合理分组管理API非常重要。可以使用@Tag注解对API进行分组。
  1. // 用户管理API组
  2. @Tag(name = "用户管理", description = "用户相关的API操作")
  3. @RestController
  4. @RequestMapping("/api/users")
  5. public class UserController {
  6.     // ...
  7. }
  8. // 产品管理API组
  9. @Tag(name = "产品管理", description = "产品相关的API操作")
  10. @RestController
  11. @RequestMapping("/api/products")
  12. public class ProductController {
  13.     // ...
  14. }
  15. // 订单管理API组
  16. @Tag(name = "订单管理", description = "订单相关的API操作")
  17. @RestController
  18. @RequestMapping("/api/orders")
  19. public class OrderController {
  20.     // ...
  21. }
复制代码

2. 自定义响应模型

有时API的响应模型比较复杂,可以使用@Schema注解自定义响应模型。
  1. @Schema(description = "API通用响应")
  2. public class ApiResponse<T> {
  3.    
  4.     @Schema(description = "响应码", example = "200", requiredMode = Schema.RequiredMode.REQUIRED)
  5.     private int code;
  6.    
  7.     @Schema(description = "响应消息", example = "操作成功", requiredMode = Schema.RequiredMode.REQUIRED)
  8.     private String message;
  9.    
  10.     @Schema(description = "响应数据", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
  11.     private T data;
  12.    
  13.     @Schema(description = "时间戳", example = "2023-01-01T12:00:00Z", requiredMode = Schema.RequiredMode.REQUIRED)
  14.     private LocalDateTime timestamp;
  15.    
  16.     public ApiResponse(int code, String message, T data) {
  17.         this.code = code;
  18.         this.message = message;
  19.         this.data = data;
  20.         this.timestamp = LocalDateTime.now();
  21.     }
  22.    
  23.     // getters and setters
  24. }
复制代码

然后在控制器中使用:
  1. @Operation(
  2.     summary = "获取用户",
  3.     description = "根据用户ID获取用户详细信息"
  4. )
  5. @ApiResponses(value = {
  6.     @ApiResponse(responseCode = "200", description = "成功获取用户信息",
  7.         content = { @Content(mediaType = "application/json",
  8.             schema = @Schema(implementation = ApiResponse.class)) }),
  9.     @ApiResponse(responseCode = "404", description = "用户不存在",
  10.         content = @Content)
  11. })
  12. @GetMapping("/{id}")
  13. public ResponseEntity<ApiResponse<User>> getUserById(@PathVariable Long id) {
  14.     User user = userService.getUserById(id);
  15.     ApiResponse<User> response = new ApiResponse<>(200, "成功获取用户信息", user);
  16.     return ResponseEntity.ok(response);
  17. }
复制代码

3. 使用枚举限制参数值

对于某些参数,其值只能是预定义的几个选项,可以使用枚举来限制。
  1. @Schema(description = "排序方式")
  2. public enum SortOrder {
  3.     @Schema(description = "升序")
  4.     ASC,
  5.    
  6.     @Schema(description = "降序")
  7.     DESC
  8. }
复制代码

然后在控制器中使用:
  1. @GetMapping("/users")
  2. public ResponseEntity<List<User>> getUsers(
  3.     @Parameter(description = "排序字段", required = false, example = "createdAt")
  4.     @RequestParam(required = false) String sortBy,
  5.    
  6.     @Parameter(description = "排序方式", required = false, example = "DESC")
  7.     @RequestParam(required = false) SortOrder sortOrder) {
  8.    
  9.     List<User> users = userService.getUsers(sortBy, sortOrder);
  10.     return ResponseEntity.ok(users);
  11. }
复制代码

4. 使用@Hidden隐藏API

某些内部API或测试API不希望在Swagger UI中显示,可以使用@Hidden注解隐藏它们。
  1. import io.swagger.v3.oas.annotations.Hidden;
  2. @Hidden
  3. @GetMapping("/internal/health-check")
  4. public ResponseEntity<String> healthCheck() {
  5.     return ResponseEntity.ok("OK");
  6. }
复制代码

5. 使用@ExternalDocs添加外部文档

对于复杂的API,可能需要额外的文档说明,可以使用@ExternalDocs注解添加外部文档链接。
  1. import io.swagger.v3.oas.annotations.ExternalDocumentation;
  2. import io.swagger.v3.oas.annotations.extensions.Extension;
  3. import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
  4. @Tag(
  5.     name = "支付管理",
  6.     description = "支付相关的API操作",
  7.     externalDocs = @ExternalDocumentation(
  8.         description = "支付集成文档",
  9.         url = "https://example.com/docs/payment-integration"
  10.     )
  11. )
  12. @RestController
  13. @RequestMapping("/api/payments")
  14. public class PaymentController {
  15.     // ...
  16. }
复制代码

6. 使用@Extension添加自定义扩展

Swagger支持通过@Extension注解添加自定义扩展,这些扩展可以用于添加额外的元数据。
  1. @Operation(
  2.     summary = "处理支付",
  3.     description = "处理用户支付请求",
  4.     extensions = {
  5.         @Extension(
  6.             name = "x-rate-limit",
  7.             properties = {
  8.                 @ExtensionProperty(name = "requests", value = "100"),
  9.                 @ExtensionProperty(name = "period", value = "1h")
  10.             }
  11.         ),
  12.         @Extension(
  13.             name = "x-cost",
  14.             properties = {
  15.                 @ExtensionProperty(name = "credits", value = "5")
  16.             }
  17.         )
  18.     }
  19. )
  20. @PostMapping("/process")
  21. public ResponseEntity<PaymentResult> processPayment(@Valid @RequestBody PaymentRequest request) {
  22.     // ...
  23. }
复制代码

常见问题与解决方案

1. Swagger UI无法访问

问题:添加Swagger依赖后,访问/swagger-ui.html返回404错误。

解决方案:

1. 确认是否正确添加了依赖:<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-ui</artifactId>
   <version>1.6.14</version>
</dependency>
2. 对于Spring Boot 3.x,需要使用SpringDoc 2.x版本:<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
   <version>2.1.0</version>
</dependency>
3. 确认应用程序的主类上有@SpringBootApplication注解。
4. 尝试访问不同的路径:SpringDoc 1.x:/swagger-ui.htmlSpringDoc 2.x:/swagger-ui/index.html
5. SpringDoc 1.x:/swagger-ui.html
6. SpringDoc 2.x:/swagger-ui/index.html

确认是否正确添加了依赖:
  1. <dependency>
  2.    <groupId>org.springdoc</groupId>
  3.    <artifactId>springdoc-openapi-ui</artifactId>
  4.    <version>1.6.14</version>
  5. </dependency>
复制代码

对于Spring Boot 3.x,需要使用SpringDoc 2.x版本:
  1. <dependency>
  2.    <groupId>org.springdoc</groupId>
  3.    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4.    <version>2.1.0</version>
  5. </dependency>
复制代码

确认应用程序的主类上有@SpringBootApplication注解。

尝试访问不同的路径:

• SpringDoc 1.x:/swagger-ui.html
• SpringDoc 2.x:/swagger-ui/index.html

2. 控制器方法未显示在Swagger UI中

问题:某些控制器方法没有在Swagger UI中显示。

解决方案:

1. 确认控制器类上有@RestController或@Controller注解。
2. 确认方法上有HTTP映射注解,如@GetMapping、@PostMapping等。
3. 检查是否使用了@Hidden注解,该注解会隐藏API。
4.
  1. 如果使用了Spring Security,确保已配置允许访问Swagger相关的路径:@Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4.    @Override
  5.    protected void configure(HttpSecurity http) throws Exception {
  6.        http.authorizeRequests()
  7.            .antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
  8.            .anyRequest().authenticated();
  9.    }
  10. }
复制代码

确认控制器类上有@RestController或@Controller注解。

确认方法上有HTTP映射注解,如@GetMapping、@PostMapping等。

检查是否使用了@Hidden注解,该注解会隐藏API。

如果使用了Spring Security,确保已配置允许访问Swagger相关的路径:
  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4.    @Override
  5.    protected void configure(HttpSecurity http) throws Exception {
  6.        http.authorizeRequests()
  7.            .antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
  8.            .anyRequest().authenticated();
  9.    }
  10. }
复制代码

3. 请求/响应模型显示不正确

问题:Swagger UI中显示的请求/响应模型与实际不符。

解决方案:

1. 确保模型类上有@Schema注解,并且属性上也有相应的@Schema注解。
2.
  1. 对于泛型类型,使用@ArraySchema注解:@ArraySchema(schema = @Schema(implementation = User.class))
  2. private List<User> users;
复制代码
3.
  1. 对于复杂的继承关系,可以使用@Schema(allOf = {...})注解:@Schema(allOf = {BaseEntity.class, User.class})
  2. public class User extends BaseEntity {
  3.    // ...
  4. }
复制代码
4. 如果使用了@JsonIgnore或@JsonProperty等Jackson注解,确保它们与Swagger注解不冲突。

确保模型类上有@Schema注解,并且属性上也有相应的@Schema注解。

对于泛型类型,使用@ArraySchema注解:
  1. @ArraySchema(schema = @Schema(implementation = User.class))
  2. private List<User> users;
复制代码

对于复杂的继承关系,可以使用@Schema(allOf = {...})注解:
  1. @Schema(allOf = {BaseEntity.class, User.class})
  2. public class User extends BaseEntity {
  3.    // ...
  4. }
复制代码

如果使用了@JsonIgnore或@JsonProperty等Jackson注解,确保它们与Swagger注解不冲突。

4. 文件上传API显示不正确

问题:文件上传API在Swagger UI中显示不正确或无法测试。

解决方案:

1.
  1. 确保使用了正确的请求映射和消费类型:@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. public ResponseEntity<FileUploadResult> uploadFile(
  3.    @RequestParam("file") MultipartFile file) {
  4.    // ...
  5. }
复制代码
2. 确保参数类型是MultipartFile,并且使用了@RequestParam注解。
3.
  1. 对于多个文件上传,使用数组或集合类型:@PostMapping(value = "/batch-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. public ResponseEntity<List<FileUploadResult>> batchUploadFiles(
  3.    @RequestParam("files") MultipartFile[] files) {
  4.    // ...
  5. }
复制代码

确保使用了正确的请求映射和消费类型:
  1. @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. public ResponseEntity<FileUploadResult> uploadFile(
  3.    @RequestParam("file") MultipartFile file) {
  4.    // ...
  5. }
复制代码

确保参数类型是MultipartFile,并且使用了@RequestParam注解。

对于多个文件上传,使用数组或集合类型:
  1. @PostMapping(value = "/batch-upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. public ResponseEntity<List<FileUploadResult>> batchUploadFiles(
  3.    @RequestParam("files") MultipartFile[] files) {
  4.    // ...
  5. }
复制代码

5. 认证信息无法正确传递

问题:在Swagger UI中测试需要认证的API时,认证信息无法正确传递。

解决方案:

1.
  1. 确保已定义安全方案:@SecurityScheme(
  2.    name = "bearerAuth",
  3.    type = SecuritySchemeType.HTTP,
  4.    scheme = "bearer",
  5.    bearerFormat = "JWT"
  6. )
  7. @SpringBootApplication
  8. public class MyApp {
  9.    // ...
  10. }
复制代码
2.
  1. 在需要认证的API上添加@SecurityRequirement注解:@SecurityRequirement(name = "bearerAuth")
  2. @GetMapping("/protected")
  3. public ResponseEntity<String> getProtectedResource() {
  4.    // ...
  5. }
复制代码
3. 在Swagger UI中,点击右上角的”Authorize”按钮,输入认证信息。
4.
  1. 如果使用的是API密钥认证,确保正确配置了@SecurityScheme:@SecurityScheme(
  2.    name = "apiKey",
  3.    type = SecuritySchemeType.APIKEY,
  4.    in = SecuritySchemeIn.HEADER,
  5.    paramName = "X-API-KEY"
  6. )
复制代码

确保已定义安全方案:
  1. @SecurityScheme(
  2.    name = "bearerAuth",
  3.    type = SecuritySchemeType.HTTP,
  4.    scheme = "bearer",
  5.    bearerFormat = "JWT"
  6. )
  7. @SpringBootApplication
  8. public class MyApp {
  9.    // ...
  10. }
复制代码

在需要认证的API上添加@SecurityRequirement注解:
  1. @SecurityRequirement(name = "bearerAuth")
  2. @GetMapping("/protected")
  3. public ResponseEntity<String> getProtectedResource() {
  4.    // ...
  5. }
复制代码

在Swagger UI中,点击右上角的”Authorize”按钮,输入认证信息。

如果使用的是API密钥认证,确保正确配置了@SecurityScheme:
  1. @SecurityScheme(
  2.    name = "apiKey",
  3.    type = SecuritySchemeType.APIKEY,
  4.    in = SecuritySchemeIn.HEADER,
  5.    paramName = "X-API-KEY"
  6. )
复制代码

总结

Swagger是一个强大的API文档工具,通过注解的方式,我们可以直接在代码中描述API,自动生成交互式文档。本文详细介绍了Swagger的常用注解,包括类级别、方法级别、参数级别和模型级别的注解,并通过实际应用场景展示了如何使用这些注解。

通过合理使用Swagger注解,我们可以:

1. 提高API文档的准确性和可维护性
2. 提供交互式API文档,方便前端开发和测试
3. 减少手动编写文档的工作量
4. 促进团队协作和沟通

在实际项目中,建议遵循以下最佳实践:

1. 为API合理分组,使用@Tag注解
2. 为每个API提供清晰的描述和示例
3. 详细描述请求参数和响应模型
4. 对于需要认证的API,正确配置安全方案
5. 定期更新文档,保持与代码同步

希望本文能帮助你深入理解Swagger注解的使用,提升API开发能力。在实际应用中,不断探索和实践,你会发现Swagger的更多强大功能。
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则