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

Spring Boot 2.7.10集成Swagger-ui出现404错误求助

问题解决:Spring Boot 2.7.10 适配Swagger/OpenAPI方案

你的问题核心是Springfox 2.9.2与Spring Boot 2.7.x版本不兼容,加上配置中的@EnableWebMvc破坏了Spring Boot的自动配置,导致Swagger UI静态资源无法加载,出现404错误。以下是两种可行的解决方案:

方案一:升级到Springfox 3.0.0(适配Spring Boot 2.7.x)

Springfox 3.x修复了与高版本Spring Boot的兼容性问题,具体操作如下:

  1. 修改build.gradle依赖
    替换原有的springfox依赖为3.0.0版本:
dependencies {
    // 保留其他依赖...
    implementation 'io.springfox:springfox-boot-starter:3.0.0'
    // 移除旧的springfox-swagger2和springfox-swagger-ui依赖
}
  1. 调整SwaggerConfig配置类
    移除@EnableSwagger2和@EnableWebMvc注解(Springfox 3.x通过starter自动启用,@EnableWebMvc会禁用Spring Boot的WebMvc自动配置,导致静态资源无法访问):
package com.company.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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) // 改为OAS_30对应OpenAPI 3.0
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.company"))
                .paths(PathSelectors.ant("/api/**"))
                .build();
    }
}
  1. 配置路径匹配策略
    Spring Boot 2.6+默认使用path-pattern-matcher,而Springfox 3.x依赖传统的ant_path_matcher,需要在application.properties或application.yml中添加:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  1. 访问Swagger UI
    启动应用后,访问:http://localhost:8080/swagger-ui/(注意末尾的斜杠)或http://localhost:8080/swagger-ui/index.html

方案二:切换到SpringDoc OpenAPI(推荐,Springfox已停止维护)

Springfox自2020年起停止维护,SpringDoc是当前主流的OpenAPI实现,完美适配Spring Boot 2.7.x,操作更简单:

  1. 修改build.gradle依赖
    移除所有springfox相关依赖,添加SpringDoc的starter:
dependencies {
    // 保留其他依赖...
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.15' // 适配Spring Boot 2.7.x的稳定版本
}
  1. 移除SwaggerConfig配置类(可选)
    SpringDoc默认自动扫描所有控制器,无需额外配置类。如果需要自定义文档规则,可以添加如下配置类:
package com.company.app.config;

import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenApiConfig {
    @Bean
    public GroupedOpenApi apiGroup() {
        return GroupedOpenApi.builder()
                .group("api")
                .pathsToMatch("/api/**")
                .packagesToScan("com.company")
                .build();
    }
}
  1. 访问Swagger UI
    启动应用后,访问:http://localhost:8080/swagger-ui/index.html

原问题的关键原因

  • Springfox 2.9.2不支持Spring Boot 2.6+引入的路径匹配策略变更,导致无法正确映射Swagger UI的静态资源。
  • @EnableWebMvc注解会强制启用纯Spring MVC配置,禁用Spring Boot的WebMvc自动配置,其中包括静态资源的自动映射规则,直接导致Swagger UI页面无法访问。

内容的提问来源于stack exchange,提问作者Rza İsmayıl

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 13:35:26