springboot整合swagger,打开swagger-ui.html网页空白
配置:
打开后的网页:
1.需要引入的依赖有三个(我打不开的原因之一是第三个依赖没引):
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--swagger ui-->
<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.6</version>
</dependency>
2.如果把SwaggerConfig放到了其他模块(common)的,注意外层包路径要和调用common的service一致,并且在service的启动类加上包扫描!(就是这一点找了好久都找不到原因,其他配置在common的配置也都失效了:service类的springboot扫描不到)
service模块:
common模块:
启动类加扫描注解:
看日志了吗,有没有ERROR日志
出现这种情况可能有以下几个原因:
需要在Maven或Gradle中添加Swagger的依赖,例如:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-version}</version>
</dependency>
注意:需要将${springfox-version}替换为有效的Springfox版本号。
需要在SpringBoot的启动类上添加@EnableSwagger2
注解,并且在应用程序的配置文件(application.yml或application.properties)中配置Swagger的相关属性,例如:
springfox:
documentation:
swagger:
v2:
path: /swagger-ui.html
Swagger-UI的默认请求路径是/swagger-ui.html
,如果访问的地址不正确则会出现空白页面。请确保你输入的URL地址正确。
如果应用程序使用了安全措施,如Spring Security,则需要排除对Swagger资源的保护。可以在WebSecurityConfigurerAdapter实现类中重写configure方法,并添加如下代码:
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/v2/api-docs", "/webjars/**");
}
以上这些可能都是导致Swagger UI无法正常显示的原因,您可以根据您的具体情况进行排查。