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

如何格式化Spring Boot Actuator /info端点的构建信息及时间格式?

当然可以搞定这两个需求!针对Spring Boot 1.5.10.RELEASE这个版本,我给你分两部分说清楚具体操作:

一、自定义Git提交时间的显示格式

默认情况下,生成Git信息的git-commit-id-plugin会把提交时间输出为时间戳,我们只需要修改这个插件的配置,让它直接生成格式化后的时间字符串即可:

  1. Maven项目配置:在pom.xml中找到(或添加)git-commit-id-plugin,补充时间格式相关参数:
<plugin>
    <groupId>pl.project13.maven</groupId>
    <artifactId>git-commit-id-plugin</artifactId>
    <version>2.2.4</version> <!-- 适配1.5.x版本的稳定版本 -->
    <executions>
        <execution>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>src/main/resources/git.properties</generateGitPropertiesFilename>
        <!-- 自定义易读的时间格式,比如yyyy-MM-dd HH:mm:ss -->
        <dateFormat>yyyy-MM-dd HH:mm:ss</dateFormat>
        <!-- 可选:指定时区,避免时间偏移问题 -->
        <dateFormatTimeZone>Asia/Shanghai</dateFormatTimeZone>
    </configuration>
</plugin>
  1. Gradle项目配置:对应的插件配置如下:
plugins {
    id 'pl.project13.maven.git-commit-id' version '2.2.4'
}

gitCommitId {
    dateFormat = 'yyyy-MM-dd HH:mm:ss'
    dateFormatTimeZone = 'Asia/Shanghai'
    generateGitPropertiesFile = true
}

重新构建项目后,git.properties里的git.commit.time就会变成格式化后的字符串,/info端点返回的内容里直接显示易读的时间。

二、格式化/info端点的整体构建信息

如果想调整返回的JSON结构、添加自定义字段或修改现有字段展示,推荐通过自定义InfoContributor实现,灵活性最高:

  1. 创建自定义贡献类,实现InfoContributor接口:
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

@Component
public class CustomBuildInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        // 获取原有info端点的所有信息
        Info existingInfo = builder.build();
        Map<String, Object> existingDetails = new HashMap<>(existingInfo.getDetails());

        // 处理Git提交时间(如果之前没通过插件修改,这里手动转换时间戳)
        Map<String, Object> gitInfo = (Map<String, Object>) existingDetails.get("git");
        if (gitInfo != null) {
            Map<String, Object> formattedGit = new HashMap<>(gitInfo);
            Long timestamp = (Long) formattedGit.get("commit.time");
            if (timestamp != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
                formattedGit.put("commit.time", sdf.format(new Date(timestamp)));
            }
            existingDetails.put("git", formattedGit);
        }

        // 添加自定义构建信息字段
        Map<String, Object> customBuild = new HashMap<>();
        customBuild.put("build_env", System.getenv("BUILD_ENV") != null ? System.getenv("BUILD_ENV") : "dev");
        customBuild.put("maintainer", "your-team");
        existingDetails.put("custom_build", customBuild);

        // 更新info端点的返回内容
        builder.withDetails(existingDetails);
    }
}
  1. 简单配置方式(适合轻量需求):
    如果只是想调整字段名称或过滤字段,直接在application.properties中配置info.*即可:
# 自定义Git信息展示字段
info.git.commit_id=${git.commit.id}
info.git.commit_time=${git.commit.time}
info.git.branch=${git.branch}
# 自定义项目构建信息
info.project.version=${project.version}
info.project.name=${project.name}

修改配置后记得重新构建项目,确保Git信息文件被更新,重启服务后访问/info就能看到格式化后的内容了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:58:37