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

Spring Boot 2.7集成Springfox失败:缺失RepositoryRestConfiguration Bean

解决Spring Boot 2.7整合Swagger(SpringFox)的Bean缺失问题

核心问题定位

报错Parameter 0 of constructor in springfox.documentation.spring.data.rest.BasePathAwareServicesProvider required a bean of type 'org.springframework.data.rest.core.config.RepositoryRestConfiguration' that could not be found,本质是SpringFox的Spring Data Rest自动配置模块在找不到RepositoryRestConfiguration bean时抛出的错误,触发原因是项目中间接引入了spring-boot-starter-data-rest依赖。


第一步:移除间接引入的spring-boot-starter-data-rest依赖

你未直接引入该依赖,但它出现在有效POM中,说明是其他依赖的传递依赖,按以下步骤处理:

1. 定位依赖来源

执行Maven命令分析依赖树,找到引入该依赖的根源:

mvn dependency:tree -Dincludes=org.springframework.boot:spring-boot-starter-data-rest

输出会类似:

[INFO] com.yourcompany:your-project:jar:1.0.0
[INFO] \- some-third-party-dependency:jar:x.x.x:compile
[INFO]    \- org.springframework.boot:spring-boot-starter-data-rest:jar:2.7.x:compile

这里的some-third-party-dependency就是传递引入的源头。

2. 排除传递依赖

在POM中找到对应的依赖,添加<exclusions>节点排除该依赖:

<dependency>
    <groupId>xxx</groupId> <!-- 上述找到的依赖groupId -->
    <artifactId>xxx</artifactId> <!-- 上述找到的依赖artifactId -->
    <version>xxx</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </exclusion>
    </exclusions>
</dependency>

第二步:适配Spring Boot 2.7的Swagger配置调整

即使移除了依赖,Spring Boot 2.7和SpringFox 3.0.0仍存在兼容性问题,需调整配置:

1. 修改SpringFox配置类

  • 替换@EnableSwagger2为@EnableOpenApi(SpringFox 3.0推荐注解)
  • 排除Spring Data Rest自动配置(避免残留问题)
  • 适配路径匹配策略(Spring Boot 2.7默认用pathPatternParser,SpringFox暂不兼容)

调整后的配置类:

@Configuration
@EnableOpenApi
@Profile({"!prod && swagger"})
@EnableAutoConfiguration(exclude = {SpringDataRestConfiguration.class})
public class SpringFoxConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yourcompany.yourproject")) // 限定扫描业务包,减少冗余
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("OUR SERVICE")
                .description("Our service endpoints")
                .version("1.0")
                .build();
    }

    // 解决路径匹配冲突问题
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping);
    }

    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
}

2. 修改application.yml配置

添加路径匹配策略配置:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher # 切换为SpringFox兼容的匹配策略
  main:
    allow-bean-definition-overriding: true
  # 保留原有其他配置...

3. 简化依赖

springfox-boot-starter已包含springfox-swagger-ui,可移除重复依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

备选方案:改用SpringDoc OpenAPI

若SpringFox兼容性问题仍无法解决,推荐使用SpringDoc OpenAPI——专为Spring Boot 2.7+设计的Swagger替代方案,配置更简洁:

1. 添加依赖

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.14</version>
</dependency>

2. 基础配置(可选)

在application.yml中添加:

springdoc:
  swagger-ui:
    path: /swagger-ui.html
  api-docs:
    path: /v3/api-docs

无需额外配置类,启动后直接访问http://localhost:8080/swagger-ui.html即可。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 08:12:05