[TOC] # swagger-spring-boot-starter 重构[05.swagger-core模块](33.swagger-core%E6%96%87%E6%A1%A3.md)代码 ## api文档 Swagger - 前后端分离后的契约 更符合前后分离的swagger-ui [swagger-bootstrap-ui](https://gitee.com/xiaoym/swagger-bootstrap-ui) 封装swagger依赖,方便使用 pom核心依赖 ``` <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.open.capacity</groupId> <artifactId>inner-intergration</artifactId> <version>2.0.1</version> </parent> <artifactId>swagger-spring-boot-starter</artifactId> <description>API文档支撑</description> <properties> <swagger.version>2.9.2</swagger.version> <swagger.m.version>1.0.6</swagger.m.version> </properties> <dependencies> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-bean-validators</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.1</version> </dependency> </dependencies> </project> ``` 使用方法 ``` <!-- swagger --> <dependency> <groupId>com.open.capacity</groupId> <artifactId>swagger-spring-boot-starter</artifactId> </dependency> ``` ## SwaggerConfig 自动形成swagger文档核心代码 以认证中心代码为例 ![](https://img.kancloud.cn/1f/72/1f729918c803ba1ab76a88c08343f9c3_1406x590.png) ``` @Component @Configuration @EnableSwagger2 public class SwaggerConfig implements WebMvcConfigurer { @Bean public Docket createRestApi() { ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<>(); tokenPar.name("Authorization").description("令牌"). modelRef(new ModelRef("string")). parameterType("header").required(false).build(); ParameterBuilder clientPar = new ParameterBuilder(); clientPar.name("client_id").description("应用ID"). modelRef(new ModelRef("string")). parameterType("header").required(false).build(); ParameterBuilder secretPar = new ParameterBuilder(); secretPar.name("client_secret").description("应用密钥"). modelRef(new ModelRef("string")). parameterType("header").required(false).build(); pars.add(tokenPar.build()); pars.add(clientPar.build()); pars.add(secretPar.build()); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() // .apis(RequestHandlerSelectors.basePackage("com.open.capacity")) .apis(RequestHandlerSelectors.any()) .paths( input -> PathSelectors.regex("/oauth/client.*").apply(input) || PathSelectors.regex("/oauth/user.*").apply(input) || PathSelectors.regex("/oauth/get.*").apply(input) || PathSelectors.regex("/oauth/userinfo.*").apply(input) || PathSelectors.regex("/oauth/remove.*").apply(input) || PathSelectors.regex("/oauth/refresh/token.*").apply(input)|| PathSelectors.regex("/oauth/token/list.*").apply(input)|| PathSelectors.regex("/clients.*").apply(input)|| PathSelectors.regex("/services.*").apply(input)|| PathSelectors.regex("/redis.*").apply(input) ) // .paths(PathSelectors.any()) .build().globalOperationParameters(pars); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("认证中心swagger接口文档").description("认证中心swagger接口文档").version("1.0").build(); } @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/"); resolver.setSuffix(".html"); return resolver; } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); return messageSource; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // super.addResourceHandlers(registry); registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } } ``` ## 访问网关swagger-ui [http://127.0.0.1:9200/swagger-ui.html](http://106.13.3.200:9200/swagger-ui.html) ![](https://img.kancloud.cn/33/2f/332f836695f69b5afdd4936b567477d9_1904x517.png) ## 访问认证中心swagger-ui [http://127.0.0.1:8000/doc.html](http://106.13.3.200:8000/doc.html)(http://127.0.0.1:8000/api-auth/doc.html) ``` 注意查看认证中心代码 ``` ![](https://img.kancloud.cn/37/48/37487cfd00bcc569d9222c9bab34a9c2_1701x493.png) ``` 如果有上下文路径 ## 接口地址 http://127.0.0.1:8000/api-auth/doc.html ``` ![](https://img.kancloud.cn/38/c2/38c2ba225f0b1f4a7cfef0770cddee49_1874x981.png) ![](https://img.kancloud.cn/a3/0a/a30a807ad336f550ff416d4619a9f243_1917x607.png) ## 访问用户中心swagger-ui ![](https://img.kancloud.cn/6e/20/6e202e8f49b6de8f4af274d16925b92e_1885x428.png)