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

Swagger Editor生成Spring代码后访问localhost:8080出现404错误的解决咨询

解决Swagger UI访问404的问题

我来帮你一步步排查这个问题,结合Swagger和Spring Boot的常见配置坑,你可以按下面的步骤尝试解决:


1. 先确认Swagger的版本及依赖是否正确

Swagger 2.x和3.x的依赖、UI路径都不一样,这是最容易踩的坑:

  • 如果是2.x版本,需要确保pom.xml(或build.gradle)里引入了这两个依赖:
    <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>
    
  • 如果是3.x版本(适配Spring Boot 2.6+),只需要引入starter依赖:
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    

2. 检查Swagger配置类是否生效

生成的代码里应该有SwaggerConfig类,确认它的注解和配置是否正确:

  • 2.x版本需要加@EnableSwagger2注解:
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    // 替换成你实际的controller包路径
                    .apis(RequestHandlerSelectors.basePackage("com.example.petstore.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    
  • 3.x版本需要加@EnableOpenApi注解:
    @Configuration
    @EnableOpenApi
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.OAS_30)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.petstore.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    

如果配置类不存在,手动创建一个,确保包路径能扫描到你的Controller。

3. 修正HomeController的重定向路径

不同版本的Swagger UI路径不一样,你的重定向可能和版本不匹配:

  • 2.x版本的UI路径是/swagger-ui.html,你的代码是对的,但建议改成绝对路径避免相对路径问题:
    @Controller
    public class HomeController {
        @RequestMapping(value = "/")
        public String index() {
            // 加上开头的/变成绝对路径
            return "redirect:/swagger-ui.html";
        }
    }
    
  • 3.x版本的UI路径是/swagger-ui/index.html,需要修改重定向:
    @Controller
    public class HomeController {
        @RequestMapping(value = "/")
        public String index() {
            return "redirect:/swagger-ui/index.html";
        }
    }
    

4. 测试原始UI路径,排除重定向问题

先跳过HomeController,直接访问Swagger UI的原始路径:

  • 2.x:http://localhost:8080/swagger-ui.html
  • 3.x:http://localhost:8080/swagger-ui/index.html
    如果这个路径能正常打开,说明是HomeController的重定向配置问题;如果还是404,继续检查下面的配置。

5. 检查静态资源和Security配置

  • 静态资源配置:如果项目自定义了静态资源路径,需要确保Swagger的静态文件能被访问到,在application.properties里添加:
    spring.mvc.static-path-pattern=/**
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    
  • Security配置:如果项目集成了Spring Security,需要放行Swagger相关路径,避免被拦截:
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    // 2.x版本放行路径
                    .antMatchers("/swagger-ui.html", "/v2/api-docs", "/swagger-resources/**", "/webjars/**")
                    // 3.x版本替换成下面的路径
                    // .antMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**", "/webjars/**")
                    .permitAll()
                    .anyRequest().authenticated();
        }
    }
    

按照上面的步骤排查,应该能解决你的404问题。如果还是不行,可以检查项目启动日志,看有没有Swagger相关的报错信息,比如依赖冲突、包扫描不到等。

内容的提问来源于stack exchange,提问作者Www

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 18:52:30