You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.5.14集成SpringFox 2.8.0遇/swagger-ui.html 404问题

解决方案

针对Spring Boot 2.5.14搭配SpringFox 2.8.0出现的/swagger-ui.html 404问题,可按以下步骤排查解决:

1. 启用Swagger2核心注解

在Spring Boot启动类上添加@EnableSwagger2注解,这是激活SpringFox Swagger2的必要前提:

@SpringBootApplication
@EnableSwagger2
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

2. 配置Swagger静态资源映射

Spring Boot 2.5+的静态资源规则与SpringFox 2.x不兼容,需手动配置Swagger UI的资源映射路径。创建WebMvc配置类:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

3. 修改路径匹配策略

Spring Boot 2.5+默认使用的PathPatternParser与SpringFox 2.x依赖的AntPathMatcher不兼容,需在配置文件中调整匹配策略:

application.properties

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

application.yml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

4. 放行Swagger相关路径(若集成Spring Security)

如果项目使用Spring Security,需确保Swagger相关路径不被拦截,在Security配置类中添加放行规则:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/swagger-ui.html", "/v2/api-docs", "/swagger/**", "/webjars/**")
                .permitAll()
                .anyRequest()
                .authenticated();
    }
}

5. 校验Docket路径规则

你的Docket配置中使用了.paths(regex("/api/.*")),需确保所有需要生成文档的接口都在/api/路径下;若规则过于严格导致无接口被扫描,虽不会直接引发404,但会影响Swagger文档内容。

完成以上配置后重启项目,重新访问/swagger-ui.html即可正常加载页面。

内容的提问来源于stack exchange,提问作者dev-rifaii

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 15:35:24