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

Spring Boot集成Swagger UI遇404错误,请求协助排查

问题排查与解决方案

核心问题分析

你遇到的404错误主要源于三个关键点:

  • Springfox 3.0.0依赖重复引入,引发配置冲突
  • 配置类使用了过时的DocumentationType.SWAGGER_2,与Springfox 3.x适配的OpenAPI 3.0规范不匹配
  • 若项目基于Spring Boot 3.x,Springfox 3.0.0不兼容Jakarta EE API(错误栈中出现jakarta.servlet,说明属于此场景),这是核心冲突点

分步解决方案

1. 清理重复依赖

springfox-boot-starter已包含springfox-swagger2和springfox-swagger-ui的全部功能,删除pom.xml中重复的依赖,仅保留starter:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2. 修正Swagger配置类

将DocumentationType.SWAGGER_2改为DocumentationType.OAS_30,同时移除不必要的@EnableWebMvc(Spring Boot环境下无需手动开启,自动配置即可):

package com.covoit.covoitbackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.covoit.covoitbackend.RestService"))
                .paths(PathSelectors.any()) // 替换原正则匹配,更简洁通用
                .build()
                .apiInfo(apiInfoMetaData());
    }

    private ApiInfo apiInfoMetaData() {
        return new ApiInfoBuilder()
                .title("Web Service covoit")
                .description("API Endpoint in persistence DB Covoit")
                .contact(new Contact("Dev-Team", "", "dev-team@gmail.com"))
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("1.0.0")
                .build();
    }
}

3. Spring Boot 3.x适配方案(关键)

如果项目是Spring Boot 3.x版本,Springfox 3.0.0完全不兼容Jakarta EE规范,必须替换为当前维护的springdoc-openapi工具:

替换依赖:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.2.0</version>
</dependency>

删除原SwaggerConfig类,无需额外配置(默认自动扫描所有@RestController)

访问地址保持:http://localhost:9090/swagger-ui/index.html

4. 验证控制器包路径

确认com.covoit.covoitbackend.RestService包下存在标注@RestController或@Controller的类,否则Docket无法扫描到接口,会导致/v3/api-docs返回空或404。


验证步骤

  1. 重启应用
  2. 先访问http://localhost:9090/v3/api-docs,确认能返回JSON格式的接口文档
  3. 再访问Swagger UI地址,即可正常加载界面

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:53:27