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

在不同端口暴露Actuator端点时遇CorsEndpointProperties Bean缺失问题求助

Actuator独立端口配置报错解决

问题描述

已正常暴露Actuator端点,添加management.server.port=9001配置将Actuator切换到独立端口后,启动报错:

com.finture.pp.dm.ewmservices.configuration.SpringFoxConfig类的webEndpointServletHandlerMapping方法的第4个参数需要类型为org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties的Bean,但该Bean未找到。
操作建议:考虑在配置中定义类型为org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties的Bean。

自定义配置类代码:

@Configuration
public class SpringFoxConfig {

  @Bean
  public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
      WebEndpointsSupplier webEndpointsSupplier,
      ServletEndpointsSupplier servletEndpointsSupplier,
      ControllerEndpointsSupplier controllerEndpointsSupplier,
      EndpointMediaTypes endpointMediaTypes,
      CorsEndpointProperties corsProperties,
      WebEndpointProperties webEndpointProperties,
      Environment environment) {
//Do something
}

application.yml配置:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      group:
        readiness:
          include: db, rabbit
      show-details: ALWAYS
      probes:
        enabled: true
  server:
    port: 9001

问题根源

配置management.server.port后,Spring Boot会启动独立的管理Web服务器及专用Spring上下文,Actuator的CorsEndpointProperties等配置Bean仅存在于这个管理上下文中。而自定义的webEndpointServletHandlerMapping Bean注册在主应用上下文,尝试依赖管理上下文的Bean,导致找不到依赖报错。

解决方案

方案一:移除自定义的webEndpointServletHandlerMapping Bean

Actuator在独立端口运行时,会自动创建自身的端点映射Bean,无需手动定义。删除这个自定义Bean后,主应用上下文与管理上下文互不干扰,即可正常启动并让Actuator运行在9001端口。

方案二:手动注册依赖Bean(不推荐)

如果必须保留自定义Bean,可在SpringFoxConfig中手动创建CorsEndpointProperties Bean:

@Bean
public CorsEndpointProperties corsEndpointProperties() {
    return new CorsEndpointProperties();
}

但这种方式可能引发后续上下文冲突问题,仅在特殊场景下使用。

结论

完全可以将Actuator暴露在与应用不同的端口,采用方案一即可顺利实现需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 07:40:22