?swagger基本注解
@Tag
- 介紹:用于給接口分組,用途類似于為接口文檔添加標簽。
- 用于:方法、類、接口。
- 常用屬性:
name
:分組的名稱@RestController @RequestMapping("/sysUser") @Tag(name = "管理員接口") public class SysUserController extends BaseController {
@Operation
- 介紹:用于描述接口的操作。
- 用于:方法。
- 常用屬性:
summary
:操作的摘要信息。description
:操作的詳細描述。? @Operation(summary = "管理員登錄", description = "根據賬號密碼進行管理員登錄")//controller層如果介紹的是body 參數 需要使用@RequestBody注解public R<String> login(@RequestBody LoginDTO loginDTO) {return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());}?
@Parameters
- 介紹:用于指定
@Parameter
注解對象數組,描述操作的輸入參數。- 用于:方法。
@Parameters(value = {@Parameter(name = "userId", in = ParameterIn.PATH, description = "用戶ID")})public R<Void> delete(@PathVariable Long userId) {return null;}
@Parameter
- 介紹:用于描述輸入參數。
- 用于:方法。
- 常用屬性:
name
:參數的名稱。in
:參數的位置,可以是path
、query
、header
、cookie
中的一種。description
:參數的描述。@Parameters(value = {@Parameter(name = "userId", in = ParameterIn.PATH, description = "用戶ID")})public R<Void> delete(@PathVariable Long userId) {return null;}
@ApiResponse
- 介紹:用于描述操作的響應結果。
- 用于:方法。
- 常用屬性:
- responseCode:響應的狀態碼。
- description:響應的描述。
@ApiResponse(responseCode = "1000", description = "操作成功")@ApiResponse(responseCode = "2000", description = "服務繁忙請稍后重試")@ApiResponse(responseCode = "3102", description = "用戶不存在")@ApiResponse(responseCode = "3103", description = "用戶名或密碼錯誤")//controller層如果介紹的是body 參數 需要使用@RequestBody注解public R<String> login(@RequestBody LoginDTO loginDTO) {return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());}
@Schema
- 介紹:用于描述數據模型的屬性。
- 用于:方法、類、接口。
- 常用屬性:
- description:響應的描述。
@Getter @Setter public class SysUserSaveDTO {@Schema(description = "用戶賬號")private String userAccount;@Schema(description = "用戶密碼")private String password; }
?為了讓SwaggerConfig生效(外部bean讓Spring能掃描到)
在oj-common-swagger模塊下的 resources 下創建
META-INF.spring包
再創建org.springframework.boot.autoconfigure.AutoConfiguration.imports?件
在里面寫上路徑
com.qyy.swagger.SwaggerConfig;
生成當前接口文檔的地址
服務器運行之后,在瀏覽器輸入地址:例如我的地址就是
http://localhost:1208/swagger-ui/index.html