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

Spring Boot集成Swagger后swagger-ui访问404其余接口正常问题求助

问题解决方法

1. 优先检查访问路径

你当前使用的Springfox 2.9.2版本的Swagger UI默认访问路径不是/swagger-ui,而是/swagger-ui.html,请直接访问https://localhost:9444/swagger-ui.html测试,该路径是2.x版本的默认UI入口,/swagger-ui路径是Springfox 3.x+ 或SpringDoc等新版本组件的默认路径。

2. 配置静态资源映射

如果修改路径后仍返回404,说明Spring MVC没有正确映射Swagger UI的静态资源,需要新增Web层配置类,手动添加资源映射规则:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class SwaggerWebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 映射swagger-ui.html静态资源
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        // 映射swagger相关的webjars资源
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

3. 检查其他可能拦截的配置

  • 如果项目中配置了server.servlet.context-path上下文路径,需要将上下文路径拼接到访问地址前,例如上下文路径为/api时,访问地址为https://localhost:9444/api/swagger-ui.html
  • 如果项目存在自定义拦截器、过滤器,需要将以下路径加入放行名单:
    • /swagger-ui.html
    • /v2/api-docs
    • /swagger-resources/**
    • /webjars/**

关于YAML配置的说明

不需要额外的YAML配置即可正常使用Swagger2,除非你有自定义静态资源路径、接口扫描规则等个性化需求,常规场景无需额外配置yaml。


内容的提问来源于stack exchange,提问作者コンドン

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 14:15:08