依赖版本信息
Spring boot 2.1.0.RELEASE
swagger2 2.7.0
1- mvn 配置  pom. 包引入
 1 <!--swagger2依赖-->
 2 <dependency>
 3 <groupId>io.springfox</groupId>
 4 <artifactId>springfox-swagger2</artifactId>
 5 <version>2.7.0</version>
 6 </dependency>
 7 <dependency>
 8 <groupId>io.springfox</groupId>
 9 <artifactId>springfox-swagger-ui</artifactId>
10 <version>2.7.0</version>
11 </dependency>
12 <!--swagger2美化插件依赖-->
13 <dependency>
14 <groupId>com.github.xiaoymin</groupId>
15 <artifactId>swagger-bootstrap-ui</artifactId>
16 <version>1.6</version>
17 </dependency>
2-配置Config
在Application.java同级目录创建 Swagger2Config.java 内容
\"\"
 1 package com.muyuer.springdemo;
 2 
 3 import org.spring work.context.annotation.Bean;
 4 import org.spring work.context.annotation.Configuration;
 5 import springfox.documentation.builders.ApiInfoBuilder;
 6 import springfox.documentation.builders.PathSelectors;
 7 import springfox.documentation.builders.RequestHandlerSelectors;
 8 import springfox.documentation.service.ApiInfo;
 9 import springfox.documentation.spi.DocumentationType;
10 import springfox.documentation.spring.web.plugins.Docket;
11 import springfox.documentation.swagger2.annotations.EnableSwagger2;
12 
13 @Configuration
14 @EnableSwagger2
15 public class Swagger2Config {
16 
17     @Bean
18     public Docket createRestApi() {
19         return new Docket(DocumentationType.SWAGGER_2)
20                 .apiInfo(apiInfo())
21                 .select()
22                 .apis(RequestHandlerSelectors. Package(\"com.muyuer.springdemo.controller\"))
23                 .paths(PathSelectors.any())
24                 .build();
25     }
26 
27     private ApiInfo apiInfo() {
28         return new ApiInfoBuilder()
29                 . (\"xx项目 RESTful APIs\")
30                 .de ion(\"xx项目后台api接口文档\")
31                 .version(\"1.0\")
32                 .build();
33     }
34 
35 }
3-遇到的问题
开始使用其它版本swagger出现过以下错误 
{\"code\":404,\"message\":\"接口 [/swagger-ui.html] 不存在\"}
网上搭建答案是
添加 WebMvcConfigurer.java内容如下 
//swagger2配置
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
registry.addResourceHandler(\"swagger-ui.html\")
.addResourceLocations(\"classpath:/ -INF/resources/\");
 
registry.addResourceHandler(\"/webjars/**\")
.addResourceLocations(\"classpath:/ -INF/resources/webjars/\");
}

自己实际测试添加了这些代码并未生效

重新建了项目引用上面版本的Spring boot 2.1.0.RELEASE swagger2 2.7.0包后即成功了。

收藏 打印