確保增加?swagger 依賴
pom.xml
<!-- Swagger --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.5.0</version></dependency>
在瀏覽器打開:http://localhost:8080/swagger-ui/index.html,出現空白頁
再點擊刷新,顯示該網頁無法正常遠行
控制臺上輸出 401 狀態
根據瀏覽器控制臺的錯誤信息?"Failed to load resource: the server responded with a status of 401 ()",這表明您的請求被服務器拒絕,原因是身份驗證失敗 (401 Unauthorized)。
路徑被攔截或安全配置
-
安全框架攔截:若項目使用了 Spring Security,需放行 Swagger 相關路徑:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() // 放行路徑.anyRequest().authenticated();}
}
-
檢查過濾器/攔截器:自定義過濾器或攔截器可能阻止訪問,需排除 Swagger 路徑。
原來項目中的攔截器:
WebConfig.java
package com.weiyu.config;import com.weiyu.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** Spring MVC 配置類*/
@Configuration
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;/*** 增加攔截器,將攔截器添加到 Spring MVC** @param registry 攔截器對象*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 增加登錄攔截器,默認攔截所有的請求registry.addInterceptor(loginInterceptor)// 請求白名單,不攔截的請求.excludePathPatterns(// 登錄接口"/account/login",// 注冊接口"/user/register");}
}
將?swagger 增加進白名單
package com.weiyu.config;import com.weiyu.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** Spring MVC 配置類*/
@Configuration
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate LoginInterceptor loginInterceptor;/*** 增加攔截器,將攔截器添加到 Spring MVC** @param registry 攔截器對象*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 增加登錄攔截器,默認攔截所有的請求registry.addInterceptor(loginInterceptor)// 請求白名單,不攔截的請求.excludePathPatterns(// 登錄接口"/account/login",// 注冊接口"/user/register",// Swagger"/swagger-ui/**","/swagger-resources/**","/v3/api-docs/**","/webjars/**","/swagger-ui.html");}
}
重啟應用,訪問效果:
如果你無法訪問?http://localhost:8080/swagger-ui.html
,可能是以下原因導致的。請按步驟排查:
1.?服務未運行或端口錯誤
-
檢查服務是否啟動:確保你的 Spring Boot 應用已成功啟動(查看控制臺日志,確認無報錯)。
-
驗證端口:檢查應用是否運行在?
8080
?端口(默認端口)。若端口被修改(如?application.properties
?中設置?server.port=9090
),需訪問?http://localhost:9090/swagger-ui.html
。
2.?Swagger 依賴未正確配置
-
確認依賴:確保項目中已添加 Swagger 依賴(根據版本選擇):
-
Springdoc (OpenAPI 3)(推薦):
xml
復制
下載
運行
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.5.0</version> <!-- 檢查最新版本 --> </dependency>
-
Springfox (Swagger 2)(舊版):
xml
復制
下載
運行
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version> </dependency>
-
-
清理構建:更新依賴后執行?
mvn clean install
?或?gradle clean build
。
3.?路徑被攔截或安全配置
-
安全框架攔截:若項目使用了 Spring Security,需放行 Swagger 相關路徑:
java
復制
下載
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() // 放行路徑.anyRequest().authenticated();} }
-
檢查過濾器:自定義過濾器或攔截器可能阻止訪問,需排除 Swagger 路徑。
4.?上下文路徑(Context Path)
-
若設置了?
server.servlet.context-path
(如?server.servlet.context-path=/api
),Swagger 的路徑會變為:text
復制
下載
http://localhost:8080/api/swagger-ui.html
5.?訪問備用路徑
-
Springdoc 的備用路徑:
-
http://localhost:8080/swagger-ui/index.html
-
http://localhost:8080/swagger-ui/
-
-
Springfox 的路徑:
-
http://localhost:8080/swagger-ui/
(注意結尾斜杠)
-
6.?檢查 Swagger 配置類
-
Springfox?需要配置類(舊版):
java
復制
下載
@Configuration @EnableSwagger2 public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();} }
-
Springdoc?無需配置(默認啟用)。
7.?瀏覽器緩存或代理問題
-
清除緩存:使用 Ctrl + Shift + R(Windows)或 Cmd + Shift + R(Mac)強制刷新頁面。
-
關閉代理:瀏覽器代理或 VPN 可能導致 localhost 訪問異常。
8.?驗證 API 文檔 JSON 是否生成
訪問以下路徑,確認是否返回 JSON:
-
Springdoc:?
http://localhost:8080/v3/api-docs
-
Springfox:?
http://localhost:8080/v2/api-docs
若返回 JSON,則 Swagger 已生效,問題在 UI 路徑;若未返回,檢查依賴和配置。
9.?其他可能原因
-
端口沖突:其他程序占用了?
8080
?端口(通過?netstat -ano | findstr :8080
?檢查并終止進程)。 -
防火墻限制:確保防火墻允許?
8080
?端口的本地訪問。 -
項目未編譯:代碼修改后未重新編譯,導致 Swagger 未生成。
快速測試步驟
-
啟動應用,觀察日志是否顯示 Swagger 初始化信息。
-
直接訪問?
http://localhost:8080/v3/api-docs
(Springdoc)或?http://localhost:8080/v2/api-docs
(Springfox)。 -
若返回 JSON,嘗試訪問?
http://localhost:8080/swagger-ui/index.html
。
通過以上排查,通常可解決 Swagger 無法訪問的問題。