解决访问swaggerUI接口文档显示basic-error-controler问题
原文地址:https://www.cnblogs.com/longronglang/p/9045559.html
问题描述
使用swagger生成接口文档后,访问http://localhost:8888/swagger-ui.html#/,显示如下:

去除basic-error-controller方法:
注意《不显示错误的接口地址》的配置
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger2的配置类,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
* EnableSwagger2:开启Swagger使用
* Configuration:表示这是一个配置类
* @author :
* @date : 2021-10-18 17:30
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createTestApi() {// 创建API基本信息
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 对所有api进行监控
.apis(RequestHandlerSelectors.any())
//不显示错误的接口地址
.paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
//扫描所有的包 可以扫描指定的包 .apis(RequestHandlerSelectors.basePackage("具体controller所在的包"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建API的基本信息,这些信息会在Swagger UI中进行显示
* @return API的基本信息
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
//标题
.title("Meng的接口测试文档")
// API描述
.description("Swagger-接口文档")
//接口的版本
.version("1.0.0")
.build();
}
}
再次访问如下:

PS:pom依赖
<!--Swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-bootstrap-ui增强ui -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.4</version>
</dependency>
本文档介绍如何通过配置Springfox Swagger2来移除SwaggerUI中显示的`basic-error-controller`接口,以便只展示有效接口。通过在Docket配置中添加`paths(Predicates.not(PathSelectors.regex(/error.*)))`,可以排除错误路径的监控。同时,提供了相关依赖pom.xml文件,以确保正确引入Swagger和Swagger-Bootstrap-UI。
2921

被折叠的 条评论
为什么被折叠?



