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

如何在Swagger UI中暴露Actuator端点?启动遇循环依赖问题

解决开启show-actuator后的循环依赖问题

问题分析

从循环依赖链能看出,开启show-actuator后,SpringDoc的actuatorProvider依赖Gateway端点Bean,而Gateway的FilterFactory又依赖WebFlux配置,形成闭环。这是Spring Boot 3.2.x + springdoc 2.0.x + Spring Cloud Gateway组合下的版本兼容问题。

解决方案

方案1:升级SpringDoc版本

springdoc-openapi 2.2.0及以上版本已修复该循环依赖问题,直接升级依赖版本即可:

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

升级后保留原有的show-actuator: true配置,应用可正常启动,Actuator端点也会自动暴露在Swagger UI中。

方案2:延迟初始化相关Bean

若暂时无法升级版本,可通过延迟初始化SpringDoc相关Bean打破循环,添加如下配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springdoc.webflux.ui.SwaggerConfig;

@Configuration
public class SpringDocLazyConfig {

    @Bean
    @Lazy
    public SwaggerConfig.SwaggerWebFluxConfigurer swaggerWebFluxConfigurer() {
        return new SwaggerConfig().swaggerWebFluxConfigurer();
    }
}

注:不推荐全局开启延迟初始化(spring.main.lazy-initialization: true),可能影响其他Bean的加载逻辑。

方案3:手动配置Actuator端点到Swagger UI

若以上方案均不适用,可关闭show-actuator,手动将Actuator端点加入OpenAPI文档:

  1. 关闭自动暴露配置:
springdoc:
  swagger-ui:
    enabled: true
    path: /swagger-ui
    config-url: /swagger-ui/config
  show-actuator: false
  1. 添加自定义配置类注册Actuator端点:
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.paths.PathItem;
import io.swagger.v3.oas.models.paths.Paths;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ActuatorSwaggerConfig {

    @Bean
    public OpenApiCustomizer actuatorOpenApiCustomizer(PathMappedEndpoints pathMappedEndpoints) {
        return openApi -> {
            Paths paths = new Paths();
            pathMappedEndpoints.getAllPaths().forEach(path -> {
                paths.addPathItem("/actuator" + path, new PathItem());
            });
            openApi.setPaths(paths);
        };
    }
}

此方式需手动维护端点文档信息,但可彻底规避循环依赖。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 09:07:51