SpringBoot整合Swagger2/Swagger3之?dāng)r截器配置方式
更新時間:2026年06月29日 09:24:33 作者:Ju3tinZ2
這段文章詳細(xì)介紹了Springfox Swagger 3.0的配置方法,包括前端資源路徑設(shè)置和POM依賴配置,并提供了AntPath路徑匹配規(guī)則說明,幫助大家更好地理解和應(yīng)用Swagger文檔生成
1. swagger2.0
1.1 POM依賴
<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>1.2 Swagger設(shè)置
@Configuration
// 關(guān)鍵
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestAPI() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 改為自己的controller包路徑
.apis(RequestHandlerSelectors.basePackage("xx.xx.xx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("WebNovel接口文檔")
.description("WebNovel Restful 接口")
.contact(new Contact("xxUser", "#", "xxUser@.qq.com"))
.version("1.0")
.build();
}
}
1.3 攔截器排除
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
// 請求路徑排除
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor()
.addPathPatterns("/**")
.excludePathPatterns("/swagger-resources/**")
.excludePathPatterns("/webjars/**")
.excludePathPatterns("/v2/**")
.excludePathPatterns("/swagger-ui.html/**");
super.addInterceptors(registry);
}
// 資源映射增加
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
2. swagger3.0
swagger3.0前端資源路徑在springfox-swagger-ui-3.0.0.jar包中。
具體路徑:META-INF\resources\webjars\springfox-swagger-ui

2.1 POM依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>2.2 Swagger設(shè)置
@Configuration
// 關(guān)鍵
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket createRestAPI() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("xx.xx.xx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("WebNovel接口文檔")
.description("WebNovel Restful 接口")
.contact(new Contact("xxUser", "#", "xxUser@qq.com"))
.version("1.0")
.build();
}
}
2.3 攔截器排除
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
super.addResourceHandlers(registry);
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor()
.addPathPatterns("/**")
.excludePathPatterns("/swagger**/**")
.excludePathPatterns("/webjars/**")
.excludePathPatterns("/v3/**")
.excludePathPatterns("/doc.html");
super.addInterceptors(registry);
}
}
3. Ant Path匹配規(guī)則
說明:
| Wildcard | Description |
|---|---|
| ? | 匹配任意單個字符 |
| * | 匹配0或多個字符 |
| ** | 匹配0或多級目錄 |
例子:
| Path | Description |
|---|---|
| /api/** | app路徑下所有路徑 |
| /**/*.html | 任意html文件 |
| /my*/*** | my開頭的路徑下的所有路徑 |
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Redis使用RedisTemplate模板類的常用操作方式
這篇文章主要介紹了Redis使用RedisTemplate模板類的常用操作方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
MyBatis中example.createCriteria()方法的具體使用
本文詳細(xì)介紹了MyBatis的Example工具的使用方法,包括鏈?zhǔn)秸{(diào)用指定字段、設(shè)置查詢條件、支持多種查詢方式等,還介紹了mapper的crud方法、and/or方法的使用,以及如何進(jìn)行多條件和多重條件查詢,感興趣的可以了解一下2024-10-10
為IntelliJ IDEA配置JVM參數(shù)的兩種方法
在使用IntelliJ IDEA進(jìn)行Java開發(fā)時,合理配置JVM參數(shù)對于優(yōu)化項目性能和資源管理至關(guān)重要,IntelliJ IDEA提供了兩種方便的方式來設(shè)置JVM參數(shù),本文將詳細(xì)介紹這兩種方法:通過工具欄編輯配置和通過服務(wù)編輯配置,需要的朋友可以參考下2024-12-12
Springboot使用filter對response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對response內(nèi)容進(jìn)行加密方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

