Spring Boot部署Azure后Swagger UI无法访问问题求助
解决Azure部署后Swagger UI 404但API Docs正常的问题
可能的解决方案:
1. 显式配置Swagger UI路径
在application.properties或application.yml中添加springdoc的路径配置,确保UI资源的映射规则正确:
application.properties
springdoc.swagger-ui.path=/swagger-ui/index.html springdoc.swagger-ui.use-root-path=false
application.yml
springdoc: swagger-ui: path: /swagger-ui/index.html use-root-path: false
2. 放行Swagger相关资源(如果用了Spring Security)
如果项目集成了Spring Security,需要确保Swagger的静态资源和API文档路径不被安全拦截,允许匿名访问:
Spring Security 5.7之前版本(基于WebSecurityConfigurerAdapter)
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/swagger-ui/**", "/v3/api-docs/**") .permitAll() .anyRequest() .authenticated(); // 忽略API Docs路径的CSRF校验,避免请求被拦截 http.csrf().ignoringAntMatchers("/v3/api-docs/**"); } }
Spring Security 5.7+(组件式配置)
@Configuration public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(auth -> auth .requestMatchers("/swagger-ui/**", "/v3/api-docs/**") .permitAll() .anyRequest() .authenticated() ); http.csrf(csrf -> csrf.ignoringRequestMatchers("/v3/api-docs/**")); return http.build(); } }
3. 检查Azure App Service的路由配置
登录Azure门户进入你的App Service:
- 打开配置 -> 路径映射,确认没有针对
/swagger-ui/的自定义路由规则(比如重写、转发) - 确认始终HTTPS已启用,保证请求协议与本地一致
4. 清理构建缓存后重新部署
本地构建可能存在静态资源缓存问题,执行以下命令重新生成部署包后再部署到Azure:
Maven项目
mvn clean package
Gradle项目
./gradlew clean build
5. 升级springdoc-openapi-ui版本
你当前使用的1.6.14版本较旧,可能存在与Azure环境的兼容性问题。根据你的Spring Boot版本选择合适的升级版本:
- 若使用Spring Boot 2.x:升级到1.x系列的最新稳定版(比如1.7.0)
- 若使用Spring Boot 3.x:升级到2.x系列的稳定版
修改pom.xml中的依赖版本:
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.7.0</version> </dependency>
内容的提问来源于stack exchange,提问作者sp_artacus_code
相关产品推荐
相关产品推荐

