一、添加 swagger 相关依赖配置
1、添加 maven 依赖
<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>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.4</version>
</dependency>
2、添加配置文件 Swagger2Config.java
@Profile({"dev","local"})//配置只有开发,测试环境才能访问接口文档
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("接口文档")
.contact(new Contact("thanos","","cme.chang@vigekoo.com"))
.version("1.0.0")
.termsOfServiceUrl("")
.build();
}
}
3、在 ShiroConfig 添加允许访问
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/doc.html/**", "anon");
filterMap.put("/swagger-ui.html/**", "anon");
filterMap.put("/swagger-resources/**", "anon");
filterMap.put("/v2/api-docs/**", "anon");
filterMap.put("/webjars/**", "anon");
filterMap.put("/swagger-resources/configuration/ui/**", "anon");
filterMap.put("/swagger-resources/configuration/security/**", "anon");
shiroFilter.setFilterChainDefinitionMap(filterMap);
二、配置添加完毕,访问文档
1、启动项目访问 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 都报404错误,页面不存在。
what ???
查看 swagger 的依赖结构,发现无论是 doc.html 还是 swagger-ui.html,都在META-INF/resources目录下,所以我们需要手动的将静态资源路径指向这里
修改swagger配置swagger2Config.java:
@Profile({"dev","local"})//配置只有开发,测试环境才能访问接口文档
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
@Bean
public Docket createDocket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("接口文档")
.contact(new Contact("thanos","","cme.chang@vigekoo.com"))
.version("1.0.0")
.termsOfServiceUrl("")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 解决静态资源无法访问
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
修改 swagger 的配置类 swagger2Config.java 继承 WebMvcConfigurationSupport,实现 addResourceHandlers 方法,设置静态资源可访问。
设置完成后重启项目,就可以通过 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 正常访问了。
2、访问是可以正常访问了,但是每次访问都会报 java.lang.NumberFormatException: For input string: "". 的错误
这是因为:
pom引入的io.springfox:springfox-swagger-ui:2.92版本的jar 包内置为io.swagger:swagger-models包为1.5.20版本。
1.5.20版本中判断example只判断是否为null,没有判断example为空字符串""的情况所以报错。
1.5.21版本新增了判断example是否为null和""。所以排除1.5.20包重新导入1.5.21包即可解决
修改依赖:
在 io.springfox:springfox-swagger-ui 依赖中排除 io.swagger:swagger-models 包,在新添加 io.swagger:swagger-models:1.5.21 依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
重启服务 访问 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 就不会报错了