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

Spring Boot集成Swagger遇阻:访问Swagger UI出现404错误

解决Swagger UI 404问题的步骤

1. 清理冗余依赖

springfox-boot-starter:3.0.0已经包含了swagger-ui的所有内容,无需单独引入springfox-swagger-ui依赖,修改build.gradle的依赖配置:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.springfox:springfox-boot-starter:3.0.0'
    
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

2. 修正配置类的包扫描路径

你的控制器所在包为com.google.demoapp.controller,但SwaggerConfig中RequestHandlerSelectors.basePackage配置的是com.test.demoapp,这会导致Swagger无法扫描到API接口,修改配置类:

package com.google.demoapp.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.google.demoapp")) // 修改为实际包路径
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("My RESTful Service")
                .description("A simple RESTful service with Swagger documentation")
                .version("1.0")
                .contact(new Contact("Your Name", "", "your.email@example.com"))
                .build();
    }
}

注意:Swagger 3不需要@EnableSwagger2注解,仅保留@Configuration即可。

3. 使用正确的Swagger UI访问路径

Swagger 3的UI访问路径已变更,不再是/swagger-ui.html,需访问:
http://localhost:8080/swagger-ui/ 或 http://localhost:8080/swagger-ui/index.html

4. 重新构建并启动应用

执行以下命令完成重新构建与启动:

gradlew clean build -x test
gradlew bootRun

完成以上步骤后,即可正常访问Swagger UI页面。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 04:15:22