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

Spring Boot2.7.0 Actuator /info返回空体解决及自定义配置

Spring Boot 2.7.0 向Actuator /info端点添加自定义信息方案

问题根因

Spring Boot 2.6.0及以上版本(包含2.7.0)调整了/info端点的默认配置:默认关闭了从Spring Environment中读取info.*前缀属性暴露到端点的能力,并非Actuator v2版本废弃了该机制,只是默认开关状态变更,多数旧教程未提及该配置变动才会导致配置后返回空对象。
你通过@Value能正常读取info.app.version属性,说明属性本身已经被Spring正确加载,只是Actuator默认不会将这类属性加入/info的响应体。


方案1:配置文件方式(复用现有配置,仅需补1行配置)

在application.properties中补充如下配置,打开环境属性读取开关:

# 核心缺失配置:开启info前缀环境属性暴露
management.info.env.enabled=true

配置完成重启后,你之前定义的info.app.version=@project.version@就会自动出现在/info响应中,你也可以继续在配置文件中添加任意info.前缀的属性,比如:

info.app.name=我的业务应用
info.java.vendor=${java.vendor}

注意:@project.version@能正常替换的前提是maven资源过滤已开启,如果你使用spring-boot-starter-parent作为父pom,该能力默认已开启,无需额外配置。


方案2:编程式自定义(适合动态信息场景)

如果需要返回动态计算的内容(比如服务器时间、依赖状态、自定义业务指标),可以实现InfoContributor接口注册自定义信息提供者,示例代码:

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component
public class AppInfoContributor implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        // 写入自定义静态/动态内容
        builder.withDetail("biz", Map.of(
                "serverTime", System.currentTimeMillis(),
                "activeProfile", String.join(",", getActiveProfiles())
        ));
    }

    private String[] getActiveProfiles() {
        // 可注入Environment读取实际激活的配置文件
        return new String[]{"prod"};
    }
}

该方式不需要额外配置,只要将实现类注册为Spring Bean,Actuator会自动识别并将其返回的内容加入/info响应。


方案3:使用内置自动信息采集

Actuator自带了几类开箱即用的信息采集能力,按需开启配置即可:

  • 采集Java运行时信息:添加配置management.info.java.enabled=true,会自动返回Java版本、供应商、虚拟机信息
  • 采集应用构建信息:在pom.xml中给spring-boot-maven-plugin添加build-info目标,打包时会自动生成构建元数据,Actuator自动读取返回构建版本、构建时间等信息,插件配置示例:
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 采集Git提交信息:引入Git信息生成插件,打包生成git.properties文件后,添加配置management.info.git.enabled=true,即可返回当前部署代码的分支、提交ID、提交时间等信息。

验证方式

配置完成重启应用后访问/actuator/info端点,即可看到所有配置、自定义的信息,不再返回空{}对象。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 22:33:00