一、 pom文件添加依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
二、配置config
package com.xxxx.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ApiKey; import springfox.documentation.service.AuthorizationScope; import springfox.documentation.service.SecurityReference; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; @Configuration @EnableSwagger2 public class SwaggerConfig { /** * 公共模块-登录退出 * * @return */ @Bean public Docket CommonApi() { return new Docket(DocumentationType.SWAGGER_2) // 加载配置信息 .apiInfo(apiInfo()) // 加载swagger 扫描包 .select() //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.xxx.controller.common")) .paths(PathSelectors.any()) .build() .groupName("公共模块-登录退出") .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } /** * 数据看板 * * @return */ @Bean public Docket DataBoarApi() { return new Docket(DocumentationType.SWAGGER_2) // 加载配置信息 .apiInfo(apiInfo()) // 加载swagger 扫描包 .select() //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.xxx.controller.databoard")) .paths(PathSelectors.any()) .build() .groupName("数据看板") .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } /** * 报表 * * @return */ @Bean public Docket ReportFormsApi() { return new Docket(DocumentationType.SWAGGER_2) // 加载配置信息 .apiInfo(apiInfo()) // 加载swagger 扫描包 .select() //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.xxx.controller.reportforms")) .paths(PathSelectors.any()) .build() .groupName("报表") .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } /** * 系统管理 * * @return */ @Bean public Docket SystemApi() { return new Docket(DocumentationType.SWAGGER_2) // 加载配置信息 .apiInfo(apiInfo()) // 加载swagger 扫描包 .select() //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .apis(RequestHandlerSelectors.basePackage("com.xxx.controller.system")) .paths(PathSelectors.any()) .build() .groupName("系统管理") .securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } private List<ApiKey> securitySchemes() { List<ApiKey> apiKeys = new ArrayList<>(); apiKeys.add(new ApiKey("Authorization", "Authorization", "header")); return apiKeys; } private List<SecurityContext> securityContexts() { List<SecurityContext> securityContexts = new ArrayList<>(); securityContexts.add(SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("^(?!auth).*$")).build()); return securityContexts; } private List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; List<SecurityReference> securityReferences = new ArrayList<>(); securityReferences.add(new SecurityReference("Authorization", authorizationScopes)); return securityReferences; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("xxxxxx") .description("更多请咨询服务开发者Ray。") .version("1.0") .build(); } }
三、启动应用进行测试(配置的端口为80)
http://localhost/swagger-ui.html#/