我正在使用 spring boot actuator 和 git-commit-id-maven-plugin 来公开 git/build 信息,但是时间信息显示如下:
{
"git": {
"commit": {
"id": {
"full": "67ad5677d4a8ad6f02adda28c3963bee17183c20"
},
"time": {
"nano": 0,
"epochSecond": 1627493496
}
},
...
有没有办法将时间格式化为更易于阅读的格式?
我已经尝试了几件事,但都没有奏效,这就是我尝试过的:
将 dateFormat 添加到 git-commit-id-maven-plugin:
<plugin> <groupId>io.github.git-commit-id</groupId> <artifactId>git-commit-id-maven-plugin</artifactId> <version>5.0.0</version> <executions> <execution> <id>get-the-git-infos</id> <goals> <goal>revision</goal> </goals> <phase>initialize</phase> </execution> </executions> <configuration> <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat> <generateGitPropertiesFile>true</generateGitPropertiesFile> <generateGitPropertiesFilename>${basedir}/src/main/resources/git.properties</generateGitPropertiesFilename> <includeOnlyProperties> <includeOnlyProperty>^git.commit.time$</includeOnlyProperty> <includeOnlyProperty>^git.commit.id$</includeOnlyProperty> <includeOnlyProperty>^git.branch$</includeOnlyProperty> </includeOnlyProperties> </configuration> </plugin>
将这些 spring jackson 属性添加到
application.properties
文件中:spring.jackson.serialization.write-date-timestamps-as-nanoseconds=false spring.jackson.serialization.write-dates-as-timestamps=false/true spring.jackson.serialization.write-durations-as-timestamps =false/true spring.jackson.serialization.write-date-timestamps-as-nanoseconds =false/true
添加即时序列化程序:
DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendInstant(0) .toFormatter(); JavaTimeModule jtm = new JavaTimeModule(); jtm.addSerializer(Instant.class, new JsonSerializer<Instant>() { @Override public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeString(dtf.format(value)); } }); mapper.registerModule(jtm);
除了这个依赖:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
附加信息,这是我的 git.properties:
#Generated by Git-Commit-Id-Plugin
git.branch=bugfix/PLATV2-12640
git.commit.id=67ad5677d4a8ad6f02adda28c3963bee17183c20
git.commit.time=2021-07-28T12\:31\:36-0500
我正在使用这个 spring-boot 版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>