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

Spring Boot健康端点添加自定义字段不显示问题求助

自定义Spring Boot Actuator健康检查字段不显示的解决方案
  • 配置健康检查详情展示级别
    默认Spring Boot Actuator健康端点仅返回整体状态,不会展示各健康指示器的自定义字段。需在application.properties中添加以下配置开启详情展示:
# 始终显示所有详情(生产环境推荐使用when-authorized,仅授权用户可见)
management.endpoint.health.show-details=always
# 或单独开启组件详情展示
management.endpoint.health.show-components=always
  • 检查自定义HealthIndicator代码实现
    确保你在doHealthCheck方法中正确调用withDetail添加自定义字段,且类上标注了@Component让Spring扫描到该Bean:
@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // 自定义检查逻辑示例
        boolean serviceOk = true;
        if (serviceOk) {
            builder.up()
                  .withDetail("custom-service", "运行正常")
                  .withDetail("check-time", System.currentTimeMillis());
        } else {
            builder.down()
                  .withDetail("error-info", "服务连接失败");
        }
    }
}
  • 验证结果
    重启应用后访问/actuator/health,响应会包含自定义字段,示例如下:
{
  "status": "UP",
  "components": {
    "customHealthCheck": {
      "status": "UP",
      "details": {
        "custom-service": "运行正常",
        "check-time": 1718923456789
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 250685578240,
        "free": 123456789012,
        "threshold": 10485760
      }
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:24:58