springboot 集成swagger流程,弹窗、页面不显示等疑难杂症以及解决办法
集成流程
1、引入jar包(以2.7.0版本为例)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、swagger的配置类(重点),需要根据springboot版本的不同,来区别配置,否则会引起访问swagger-ui.html页面时弹窗的问题,
所以提供如下方法 查询springboot版本号,粘贴到任意一个java类中,执行这个main函数即可打印出当前springboot版本
public static void main(String[] args) {
String version = SpringBootVersion.getVersion();
System.out.println(version);
String implementationVersion = SpringApplication.class.getPackage().getImplementationVersion();
System.out.println(implementationVersion);
}
低于springboot 2.0版本的配置类: (本人的是1.5.6,所以猜测版本差异是以2.0作为分界线的,还望大佬求证。)
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
@Configuration
@EnableSwagger2
//springboot2.0之前此类不能 implements WebMvcConfigurer这个接口
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//包下的类,才生成接口文档
//.apis(RequestHandlerSelectors.basePackage("io.renren.controller"))
.paths(PathSelectors.any())
.build()
.securitySchemes(security());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("兜兜")
.description("接口文档")
.termsOfServiceUrl("https://www.renren.io")
.version("3.0.0")
.build();
}
private List<ApiKey> security() {
return newArrayList(
new ApiKey("token", "token", "header")
);
}
}
高于springboot 2.0版本的配置类:
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//加了ApiOperation注解的类,才生成接口文档
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//包下的类,才生成接口文档
//.apis(RequestHandlerSelectors.basePackage("io.renren.controller"))
.paths(PathSelectors.any())
.build()
.securitySchemes(security());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("新计算器")
.description("微信接口文档")
.termsOfServiceUrl("https://www.renren.io")
.version("3.0.0")
.build();
}
private List<ApiKey> security() {
return newArrayList(
new ApiKey("token", "token", "header")
);
}
}
至此配置类已完成!
在任意Controller类上,加@Api(tags = "测试接口"),接口加@ApiOperation("示范接口"),入下图所示
访问swagger页面(例如:localhost:8088/swagger-ui.html),即可显示出接口,如下图
疑难杂症
这里主要针对的是如下图的弹窗问题:
如果大家访问不到swagger页面,说明此页面被拦截,注意检查自己的拦截器,放行swagger-ui.html这个页面。
关于这个弹窗,网上众说纷纭,我遇到的原因是swagger相关的资源被拦截了,在此罗列出主流的解决方案,估计总有一款适合您
1、检查自己的拦截器是否放行了所有的swagger的资源,本人用的是shiro,以shiro为例,需要放行的资源如下:重启项目即可解决弹窗,可类比其他的拦截器,总之swagger的资源都需要放行
filterMap.put("/swagger-ui.html", "anon");
filterMap.put("/webjars/**", "anon");
filterMap.put("/v2/**", "anon");
filterMap.put("/swagger-resources/**", "anon");
2、springboot启动类上加注解 @EnableSwagger2 ,启动类需要继承 SpringBootServletInitializer :如下图
3、注意版本的兼容性,可试试高版本的swagger,这个原因的可能性较小
如果还有其他原因,欢迎大家提出来,予以总结
上一篇: HTML百叶窗特效
下一篇: C++简单单元测试类