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

SpringFox整合Swagger失效求助:API文档接口返回404

排查SpringFox Swagger 404问题的解决方案

根据你提供的配置和问题描述,我们一步步来定位并解决这个404问题:

1. 优先解决依赖版本冲突

你手动引入了swagger-core 1.5.3并排除了Jackson相关依赖,但这个版本和springfox-swagger2 2.6.1严重不兼容——SpringFox 2.6.1内置的swagger-core版本是1.5.20,强行指定低版本会导致序列化、API文档生成逻辑出现异常。

修改pom.xml,移除单独的swagger-core依赖:

<!-- 移除这部分单独的swagger-core依赖 -->
<!-- 
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-core</artifactId>
    <version>1.5.3</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
-->

<!-- 保留SpringFox的两个依赖即可 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

2. 修正Swagger配置类的注解

如果你的项目是Spring Boot项目,不要添加@EnableWebMvc注解——这个注解会禁用Spring Boot的WebMvc自动配置,导致请求映射规则混乱,Swagger的API文档路径无法正确注册。

修改后的SwaggerConfig类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("allocation-order-service")
                .description("domain services having persistance layer")
                .version("1.0")
                .build();
    }
}

3. 确认上下文路径与访问地址匹配

你的访问地址是http://localhost:8080/allocation-order-web/v2/api-docs,需要确认项目的上下文路径配置:

  • 检查application.properties或application.yml中是否设置了server.servlet.context-path=/allocation-order-web
  • 如果没有配置上下文路径,正确的访问地址应该是http://localhost:8080/v2/api-docs

4. 排查拦截器/过滤器的拦截规则

如果项目中使用了Spring Security或自定义过滤器,必须放行Swagger相关的路径,否则请求会被拦截返回404:

  • 以Spring Security为例,添加如下放行规则:
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            // 放行Swagger相关路径
            .antMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**")
            .permitAll()
            // 其他请求的认证规则
            .anyRequest().authenticated();
}

5. 验证请求映射是否正确注册

启动项目后,查看控制台日志,找到RequestMappingHandlerMapping输出的映射信息,确认是否存在如下条目:

Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)

如果没有这条日志,说明Swagger的API文档接口没有被正确注册,需要回到前面的步骤检查依赖和配置类。

按照上述步骤调整后,重新启动项目,再访问对应的/v2/api-docs路径,应该就能正常获取Swagger的JSON文档了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:20:13