| | |
| | | * 创建API |
| | | */ |
| | | @Bean |
| | | public Docket createRestApi() |
| | | { |
| | | return new Docket(DocumentationType.OAS_30) |
| | | // 是否启用Swagger |
| | | public Docket systemApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | // 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息) |
| | | .apiInfo(apiInfo()) |
| | | // 设置哪些接口暴露给Swagger展示 |
| | | .groupName("系统接口") // 分组名 |
| | | .apiInfo(apiInfo("系统接口")) |
| | | .select() |
| | | // 扫描所有有注解的api,用这种方式更灵活 |
| | | .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) |
| | | // 扫描指定包中的swagger注解 |
| | | // .apis(RequestHandlerSelectors.basePackage("com.ard.project.tool.swagger")) |
| | | // 扫描所有 .apis(RequestHandlerSelectors.any()) |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller.system") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()) |
| | | .pathMapping("/"); |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | |
| | | @Bean |
| | | public Docket droneApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("无人机接口") // 分组名 |
| | | .apiInfo(apiInfo("无人机管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller.work") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | @Bean |
| | | public Docket sampleApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("机场接口") // 分组名 |
| | | .apiInfo(apiInfo("机场管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller" + |
| | | ".sample") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | @Bean |
| | | public Docket mediaMTXApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("流媒体接口") // 分组名 |
| | | .apiInfo(apiInfo("流媒体管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller" + |
| | | ".vtdu") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | @Bean |
| | | public Docket deviceApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("设备接口") // 分组名 |
| | | .apiInfo(apiInfo("设备管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller" + |
| | | ".device") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | @Bean |
| | | public Docket dataApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("数据接口") // 分组名 |
| | | .apiInfo(apiInfo("数据管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller" + |
| | | ".data") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | @Bean |
| | | public Docket storageApi() { |
| | | return new Docket(DocumentationType.SWAGGER_2) |
| | | .enable(true) |
| | | .groupName("存储接口") // 分组名 |
| | | .apiInfo(apiInfo("存储管理接口")) |
| | | .select() |
| | | // 扫描指定包下,但只扫描加了 @ApiOperation 注解的方法 |
| | | .apis(requestHandler -> |
| | | requestHandler.declaringClass().getPackage().getName().startsWith("com.ard.web.controller" + |
| | | ".storage") && |
| | | requestHandler.isAnnotatedWith(io.swagger.annotations.ApiOperation.class) |
| | | ) |
| | | .paths(PathSelectors.any()) |
| | | .build() |
| | | /* 设置安全模式,swagger可以设置访问token */ |
| | | .securitySchemes(securitySchemes()) |
| | | .securityContexts(securityContexts()); |
| | | } |
| | | /** |
| | | * 安全模式,这里指定token通过Authorization头请求头传递 |
| | | */ |
| | |
| | | securityContexts.add( |
| | | SecurityContext.builder() |
| | | .securityReferences(defaultAuth()) |
| | | .operationSelector(o -> o.requestMappingPattern().matches("/.*")) |
| | | .forPaths(PathSelectors.regex("^(?!auth).*$")) |
| | | .build()); |
| | | return securityContexts; |
| | | } |
| | |
| | | .version(uavConfig.getVersion()) |
| | | .build(); |
| | | } |
| | | private ApiInfo apiInfo(String title) { |
| | | return new ApiInfoBuilder() |
| | | .title(title) |
| | | .description("API 文档") |
| | | .version("1.0") |
| | | .build(); |
| | | } |
| | | } |