SpringDoc使用

1.SpringDoc简介

SpringDoc是一款可以结合SpringBoot使用的API文档生成工具

2.SpringDoc基础使用

POM文件

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.6</version>
</dependency>

创建OpenApiConfig配置类
配置类加上注解

@Configuration
public class OpenApiConfig {
    @Bean
    public OpenAPI springShopOpenAPI() {

        //鉴权组件(随便起名的)
        SecurityScheme securityScheme = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")//固定写法
                .bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name("Authorization");

        Components components = new Components()
                .addSecuritySchemes("bearer-jwt", securityScheme);

        //鉴权限制要求(随便起名的)
        SecurityRequirement securityRequirement = new SecurityRequirement()
                .addList("bearer-jwt", Arrays.asList("read", "write"));

        return new OpenAPI()
                .info(new Info().title("医院管理系统 API")
                        .description("Spring HIS application")
                        .version("v0.0.1"))
                .components(components)
                .addSecurityItem(securityRequirement)
        ;
    }

}

Controller的代码

@Tag(name = "床位表管理模块")
@RestController
@RequestMapping("/bed")
public class BedController {

    @Operation(summary = "分页获取Bed数据",description = "pageSize每页大小,pageNum第几页")
    @GetMapping("/page")
    public Result getPage(@RequestParam("pageSize")int pageSize,
                          @RequestParam("pageNum")int pageNum) {
        LambdaQueryWrapper<Bed> queryWrapper = new LambdaQueryWrapper<>();

        return Result.success(bedService.page(new Page<Bed>(pageNum,pageSize),queryWrapper));
    }
}

Entity代码

@Data
@TableName("his_bed")
@Schema(name = "Bed对象", description = "床位表表")
public class Bed implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Schema(name = "id",description = "床位id")
    @TableId(type = IdType.ASSIGN_ID)
    private String id;
}