一站式解决swagger报错Whitelabel Error Page

发布时间 2023-04-04 09:31:48作者: 豆汤

有以下经常出现的几点会导致swagger的访问报错Whitelabel Error Page

1、POM文件配置,版本根据实际情况可变

<swagger.version>2.9.2</swagger.version>
<swagger.annotation.version>1.5.20</swagger.annotation.version>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.annotation.version}</version>
</dependency>

2、swaggerConfig配置不正确,参考配置如下:

@Configuration
@EnableSwagger2
@Profile({"dev", "qa"})
public class SwaggerConfig {

@Value("${server.servlet.context-path}")
private String contextPath;

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
//这里采用包含注解的方式来确定要显示的接口
.apis(RequestHandlerSelectors.basePackage("com.cn.xyj"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX系统-"+contextPath+" 服务restful api")
.description(contextPath+" api接口文档")
//服务条款网址
.termsOfServiceUrl("http://localhost/")
.version("1.0.0")
.contact(new Contact("authorName", "", "xxxxxxxxx@qq.com"))
.build();
}
}

2、在springBoot启动类中添加@EnableSwagger2 注解

3、添加addResourceHandlers

registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

如果解决以上几点还有报错,可能是地址URL输入错误

 

 

如果有context-path访问服务的地址http://localhost:8080/context-path/swagger-ui.html 
如果没有context-path访问:http://localhost:8080/swagger-ui.html