From 525539b342bd173d34b7abd56366e4ab56dbd7c0 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Wed, 13 Sep 2023 21:53:16 +0800 Subject: [PATCH] bugfix jackson deserialize localDatetime error (#1249) --- .../hertzbeat/alert/service/impl/AlertServiceImpl.java | 7 ++++--- .../dromara/hertzbeat/common/entity/dto/AlertReport.java | 2 +- .../java/org/dromara/hertzbeat/common/util/JsonUtil.java | 4 +++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java index dc8e459ea10..23ff1835689 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertServiceImpl.java @@ -17,7 +17,6 @@ package org.dromara.hertzbeat.alert.service.impl; -import com.fasterxml.jackson.databind.ObjectMapper; import org.dromara.hertzbeat.alert.reduce.AlarmCommonReduce; import org.dromara.hertzbeat.alert.dao.AlertDao; import org.dromara.hertzbeat.alert.dto.AlertPriorityNum; @@ -156,7 +155,8 @@ private Alert buildAlertData(AlertReport alertReport){ }else{ sb = new StringBuilder(alertReport.getContent()); } - + LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(alertReport.getAlertTime()), + ZoneId.systemDefault()); return Alert.builder() .content("Alert Center\n" + sb) .priority(alertReport.getPriority().byteValue()) @@ -164,7 +164,8 @@ private Alert buildAlertData(AlertReport alertReport){ .tags(alertReport.getLabels()) .target(alertReport.getAlertName()) .triggerTimes(1) - .gmtCreate(LocalDateTime.ofInstant(Instant.ofEpochMilli(alertReport.getAlertTime()), ZoneId.systemDefault())) + .gmtCreate(dateTime) + .gmtUpdate(dateTime) .build(); } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/dto/AlertReport.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/dto/AlertReport.java index 171562d25df..8ae4573d599 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/dto/AlertReport.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/dto/AlertReport.java @@ -50,7 +50,7 @@ public class AlertReport { private Integer alertDuration; @Schema(title = "Time when the log service receives the alarm message", description = "日志服务接收到告警消息的时间", - example = "1648889320", accessMode = READ_WRITE) + example = "1694589491000", accessMode = READ_WRITE) private long alertTime; @Schema(title = "Alarm priority. 0: high emergency alarm red 1: medium critical serious alarm Orange 2: low warning warning alarm yellow", diff --git a/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java b/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java index 3d8dfa392ba..0ddb6a90356 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; @@ -41,7 +42,8 @@ public class JsonUtil { static { OBJECT_MAPPER .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) + .registerModule(new JavaTimeModule()); } public static String toJson(Object source) {