From e4172ca15efdb68645dc5040d296d51a74e47aad Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Thu, 6 Apr 2023 18:02:27 +0800 Subject: [PATCH] use jackson instead of gson (#846) use jackson replace of gson update codeql --- .github/workflows/codeql-analysis.yml | 7 +- .../controller/AlertDefineControllerTest.java | 12 +-- .../AlertDefinesControllerTest.java | 6 +- .../controller/AlertsControllerTest.java | 6 +- .../promethus/PrometheusMatrixParser.java | 6 +- .../promethus/PrometheusVectorParser.java | 6 +- .../hertzbeat/collector/util/CollectUtil.java | 8 +- .../common/entity/alerter/Alert.java | 4 +- .../alerter/JsonMapAttributeConverter.java | 10 +-- .../hertzbeat/common/entity/job/Job.java | 4 +- .../JsonByteListAttributeConverter.java | 10 +-- .../JsonOptionListAttributeConverter.java | 10 +-- .../JsonTagListAttributeConverter.java | 19 +++-- .../hertzbeat/common/util/GsonUtil.java | 63 -------------- .../hertzbeat/common/util/JsonUtil.java | 82 +++++++++++++++++++ .../{GsonUtilTest.java => JsonUtilTest.java} | 6 +- .../manager/service/JobSchedulerInit.java | 4 +- .../service/impl/MonitorServiceImpl.java | 3 +- .../controller/AccountControllerTest.java | 8 +- .../controller/MonitorControllerTest.java | 12 +-- .../controller/MonitorsControllerTest.java | 8 +- .../NoticeConfigControllerTest.java | 16 ++-- .../manager/controller/TagControllerTest.java | 10 +-- .../manager/dao/ParamDefineDaoTest.java | 4 +- .../support/GlobalExceptionHandlerTest.java | 22 ++--- 25 files changed, 176 insertions(+), 170 deletions(-) delete mode 100644 common/src/main/java/org/dromara/hertzbeat/common/util/GsonUtil.java create mode 100644 common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java rename common/src/test/java/org/dromara/hertzbeat/common/util/{GsonUtilTest.java => JsonUtilTest.java} (85%) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index ba46e2b02c5..e9fbb82656f 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -12,11 +12,8 @@ name: "CodeQL" on: - pull_request: - branches: [ master, dev ] - paths: - - '**/*.java' - - '**/*.ts' + schedule: + - cron: '21 13 * * 4' jobs: analyze: diff --git a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefineControllerTest.java b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefineControllerTest.java index 42a46a1294a..dd091429085 100644 --- a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefineControllerTest.java +++ b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefineControllerTest.java @@ -5,7 +5,7 @@ import org.dromara.hertzbeat.common.entity.alerter.AlertDefineMonitorBind; import org.dromara.hertzbeat.common.entity.manager.Monitor; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -85,7 +85,7 @@ void addNewAlertDefine() throws Exception { // 模拟客户端往服务端发送请求 mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(this.alertDefine))) + .content(JsonUtil.toJson(this.alertDefine))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -95,7 +95,7 @@ void addNewAlertDefine() throws Exception { void modifyAlertDefine() throws Exception { mockMvc.perform(MockMvcRequestBuilders.put("/api/alert/define") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(this.alertDefine))) + .content(JsonUtil.toJson(this.alertDefine))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -126,7 +126,7 @@ void getAlertDefine() throws Exception { void deleteAlertDefine() throws Exception { mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/define/" + this.alertDefine.getId()) .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(this.alertDefine))) + .content(JsonUtil.toJson(this.alertDefine))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -136,7 +136,7 @@ void deleteAlertDefine() throws Exception { void applyAlertDefineMonitorsBind() throws Exception { mockMvc.perform(MockMvcRequestBuilders.post("/api/alert/define/" + this.alertDefine.getId() + "/monitors") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(this.alertDefineMonitorBinds))) + .content(JsonUtil.toJson(this.alertDefineMonitorBinds))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -155,4 +155,4 @@ void getAlertDefineMonitorsBind() throws Exception { .andExpect(jsonPath("$.data[0].monitor.id").value(alertDefineMonitorBinds.get(0).getMonitor().getId())) .andReturn(); } -} \ No newline at end of file +} diff --git a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefinesControllerTest.java b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefinesControllerTest.java index 80ea4839419..1c012b61e27 100644 --- a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefinesControllerTest.java +++ b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertDefinesControllerTest.java @@ -3,7 +3,7 @@ import org.dromara.hertzbeat.alert.service.AlertDefineService; import org.dromara.hertzbeat.common.entity.alerter.AlertDefine; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -126,9 +126,9 @@ public boolean matches(PageRequest pageRequestMidden) { void deleteAlertDefines() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/alert/defines") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(ids))) + .content(JsonUtil.toJson(ids))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); } -} \ No newline at end of file +} diff --git a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertsControllerTest.java b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertsControllerTest.java index 917f657d4c3..3096e3ed755 100644 --- a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertsControllerTest.java +++ b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertsControllerTest.java @@ -5,7 +5,7 @@ import org.dromara.hertzbeat.common.entity.alerter.Alert; import org.dromara.hertzbeat.common.entity.dto.AlertReport; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -158,11 +158,11 @@ void addNewAlertReport() throws Exception { MockMvcRequestBuilders .post("/api/alerts/report") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(AlertReport.builder().build())) + .content(JsonUtil.toJson(AlertReport.builder().build())) ) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}")) .andReturn(); } -} \ No newline at end of file +} diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusMatrixParser.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusMatrixParser.java index bcc78a78b24..e2c055ed99b 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusMatrixParser.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusMatrixParser.java @@ -7,7 +7,7 @@ import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol; import org.dromara.hertzbeat.common.entity.message.CollectRep; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import lombok.NoArgsConstructor; import java.math.BigDecimal; @@ -23,7 +23,7 @@ public class PrometheusMatrixParser extends AbstractPrometheusParse { @Override public Boolean checkType(String responseStr) { try { - PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class); + PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class); if (DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType())) { return true; } @@ -35,7 +35,7 @@ public Boolean checkType(String responseStr) { @Override public void parse(String resp, List aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) { - PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class); + PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class); List result = promVectorOrMatrix.getData().getResult(); for (PromVectorOrMatrix.Result r : result) { for (List value : r.getValues()) { diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusVectorParser.java b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusVectorParser.java index 57cbceb8bb4..6e9dd394ec3 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusVectorParser.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/collect/http/promethus/PrometheusVectorParser.java @@ -7,7 +7,7 @@ import org.dromara.hertzbeat.common.entity.job.protocol.HttpProtocol; import org.dromara.hertzbeat.common.entity.message.CollectRep; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import lombok.NoArgsConstructor; import java.math.BigDecimal; @@ -23,7 +23,7 @@ public class PrometheusVectorParser extends AbstractPrometheusParse { @Override public Boolean checkType(String responseStr) { try { - PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(responseStr, PromVectorOrMatrix.class); + PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class); if (DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType())) { return true; } @@ -37,7 +37,7 @@ public Boolean checkType(String responseStr) { public void parse(String resp, List aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) { boolean setTimeFlag = false; boolean setValueFlag = false; - PromVectorOrMatrix promVectorOrMatrix = GsonUtil.fromJson(resp, PromVectorOrMatrix.class); + PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class); List result = promVectorOrMatrix.getData().getResult(); for (PromVectorOrMatrix.Result r : result) { CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); diff --git a/collector/src/main/java/org/dromara/hertzbeat/collector/util/CollectUtil.java b/collector/src/main/java/org/dromara/hertzbeat/collector/util/CollectUtil.java index 8852f1ba247..dc2dc001dc7 100644 --- a/collector/src/main/java/org/dromara/hertzbeat/collector/util/CollectUtil.java +++ b/collector/src/main/java/org/dromara/hertzbeat/collector/util/CollectUtil.java @@ -17,14 +17,13 @@ package org.dromara.hertzbeat.collector.util; +import com.fasterxml.jackson.core.type.TypeReference; import com.google.gson.*; -import com.google.gson.reflect.TypeToken; import org.dromara.hertzbeat.common.entity.job.Configmap; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import lombok.extern.slf4j.Slf4j; -import java.lang.reflect.Type; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -291,8 +290,7 @@ public static JsonElement replaceSmilingPlaceholder(JsonElement jsonElement, Map Configmap param = configmap.get(key); if (param != null && param.getType() == (byte) 3) { String jsonValue = (String) param.getValue(); - Type type = new TypeToken>(){}.getType(); - Map map = GsonUtil.fromJson(jsonValue, type); + Map map = JsonUtil.fromJson(jsonValue, new TypeReference<>() {}); if (map != null) { map.forEach((name, value) -> { if (name != null && !"".equals(name.trim())) { diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/Alert.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/Alert.java index 7c86714621b..69ab7b07a85 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/Alert.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/Alert.java @@ -17,7 +17,7 @@ package org.dromara.hertzbeat.common.entity.alerter; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -142,6 +142,6 @@ public class Alert { @Override public Alert clone() { // deep clone - return GsonUtil.fromJson(GsonUtil.toJson(this), Alert.class); + return JsonUtil.fromJson(JsonUtil.toJson(this), Alert.class); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/JsonMapAttributeConverter.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/JsonMapAttributeConverter.java index b9e27defeb1..0aeae6c069b 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/JsonMapAttributeConverter.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/alerter/JsonMapAttributeConverter.java @@ -17,11 +17,10 @@ package org.dromara.hertzbeat.common.entity.alerter; -import com.google.gson.reflect.TypeToken; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; +import com.fasterxml.jackson.core.type.TypeReference; import javax.persistence.AttributeConverter; -import java.lang.reflect.Type; import java.util.Map; /** @@ -33,12 +32,11 @@ public class JsonMapAttributeConverter implements AttributeConverter attribute) { - return GsonUtil.toJson(attribute); + return JsonUtil.toJson(attribute); } @Override public Map convertToEntityAttribute(String dbData) { - Type type = new TypeToken>(){}.getType(); - return GsonUtil.fromJson(dbData, type); + return JsonUtil.fromJson(dbData, new TypeReference<>() {}); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/job/Job.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/job/Job.java index 8bc9208aa27..722a018d45d 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/job/Job.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/job/Job.java @@ -21,7 +21,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import org.dromara.hertzbeat.common.entity.manager.ParamDefine; import org.dromara.hertzbeat.common.entity.message.CollectRep; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -235,6 +235,6 @@ public void addCollectMetricsData(CollectRep.MetricsData metricsData) { @Override public Job clone() { // deep clone 深度克隆 - return GsonUtil.fromJson(GsonUtil.toJson(this), Job.class); + return JsonUtil.fromJson(JsonUtil.toJson(this), Job.class); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonByteListAttributeConverter.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonByteListAttributeConverter.java index df18c327713..2285771bd94 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonByteListAttributeConverter.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonByteListAttributeConverter.java @@ -17,11 +17,10 @@ package org.dromara.hertzbeat.common.entity.manager; -import com.google.gson.reflect.TypeToken; -import org.dromara.hertzbeat.common.util.GsonUtil; +import com.fasterxml.jackson.core.type.TypeReference; +import org.dromara.hertzbeat.common.util.JsonUtil; import javax.persistence.AttributeConverter; -import java.lang.reflect.Type; import java.util.List; /** @@ -33,12 +32,11 @@ public class JsonByteListAttributeConverter implements AttributeConverter attribute) { - return GsonUtil.toJson(attribute); + return JsonUtil.toJson(attribute); } @Override public List convertToEntityAttribute(String dbData) { - Type type = new TypeToken>(){}.getType(); - return GsonUtil.fromJson(dbData, type); + return JsonUtil.fromJson(dbData, new TypeReference<>() {}); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonOptionListAttributeConverter.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonOptionListAttributeConverter.java index 9969b158fbf..084567d6c42 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonOptionListAttributeConverter.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonOptionListAttributeConverter.java @@ -17,11 +17,10 @@ package org.dromara.hertzbeat.common.entity.manager; -import com.google.gson.reflect.TypeToken; -import org.dromara.hertzbeat.common.util.GsonUtil; +import com.fasterxml.jackson.core.type.TypeReference; +import org.dromara.hertzbeat.common.util.JsonUtil; import javax.persistence.AttributeConverter; -import java.lang.reflect.Type; import java.util.List; /** @@ -33,12 +32,11 @@ public class JsonOptionListAttributeConverter implements AttributeConverter attribute) { - return GsonUtil.toJson(attribute); + return JsonUtil.toJson(attribute); } @Override public List convertToEntityAttribute(String dbData) { - Type type = new TypeToken>(){}.getType(); - return GsonUtil.fromJson(dbData, type); + return JsonUtil.fromJson(dbData, new TypeReference<>() {}); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonTagListAttributeConverter.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonTagListAttributeConverter.java index 45215532c46..e455edde3c8 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonTagListAttributeConverter.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/JsonTagListAttributeConverter.java @@ -17,11 +17,10 @@ package org.dromara.hertzbeat.common.entity.manager; -import com.google.gson.reflect.TypeToken; -import org.dromara.hertzbeat.common.util.GsonUtil; +import com.fasterxml.jackson.core.type.TypeReference; +import org.dromara.hertzbeat.common.util.JsonUtil; import javax.persistence.AttributeConverter; -import java.lang.reflect.Type; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -35,19 +34,21 @@ public class JsonTagListAttributeConverter implements AttributeConverter attribute) { - return GsonUtil.toJson(attribute); + return JsonUtil.toJson(attribute); } @Override public List convertToEntityAttribute(String dbData) { try { - Type type = new TypeToken>(){}.getType(); - return GsonUtil.fromJson(dbData, type); + return JsonUtil.fromJson(dbData, new TypeReference<>() {}); } catch (Exception e) { // history data handler - Type type = new TypeToken>(){}.getType(); - Map map = GsonUtil.fromJson(dbData, type); - return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList()); + Map map = JsonUtil.fromJson(dbData, new TypeReference<>() {}); + if (map != null) { + return map.entrySet().stream().map(entry -> new NoticeRule.TagItem(entry.getKey(), entry.getValue())).collect(Collectors.toList()); + } else { + return null; + } } } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/util/GsonUtil.java b/common/src/main/java/org/dromara/hertzbeat/common/util/GsonUtil.java deleted file mode 100644 index 349b346b49b..00000000000 --- a/common/src/main/java/org/dromara/hertzbeat/common/util/GsonUtil.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.dromara.hertzbeat.common.util; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; - -import javax.annotation.concurrent.ThreadSafe; -import java.lang.reflect.Type; - -/** - * gson 工具类 - * @author tomsun28 - * @date 2021/10/16 20:49 - */ -@ThreadSafe -public class GsonUtil { - - private static Gson gson; - - static { - gson = new GsonBuilder().enableComplexMapKeySerialization() - .serializeNulls() - .create(); - } - - public static String toJson(Object source) { - return gson.toJson(source); - } - - public static T fromJson(String jsonStr, Class clazz) { - return gson.fromJson(jsonStr, clazz); - } - - public static T fromJson(String jsonStr, Type type) { - return gson.fromJson(jsonStr, type); - } - - public static T fromJson(JsonElement element, Class clazz) { - return gson.fromJson(element, clazz); - } - - public static JsonElement toJsonTree(Object source) { - return gson.toJsonTree(source); - } - -} 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 new file mode 100644 index 00000000000..aeffc33f419 --- /dev/null +++ b/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dromara.hertzbeat.common.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; + +import javax.annotation.concurrent.ThreadSafe; + +/** + * gson 工具类 + * @author tomsun28 + * @date 2021/10/16 20:49 + */ +@ThreadSafe +@Slf4j +public class JsonUtil { + + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + static { + OBJECT_MAPPER + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } + + public static String toJson(Object source) { + if (source == null) { + return null; + } + try { + return OBJECT_MAPPER.writeValueAsString(source); + } catch (JsonProcessingException e) { + log.error(e.getMessage(), e); + return null; + } + } + + public static T fromJson(String jsonStr, Class clazz) { + if (!StringUtils.hasText(jsonStr)) { + return null; + } + try { + return OBJECT_MAPPER.readValue(jsonStr, clazz); + } catch (Exception e) { + log.error(e.getMessage(), e); + return null; + } + } + + public static T fromJson(String jsonStr, TypeReference type) { + if (!StringUtils.hasText(jsonStr)) { + return null; + } + try { + return OBJECT_MAPPER.readValue(jsonStr, type); + } catch (Exception e) { + log.error(e.getMessage(), e); + return null; + } + } + + +} diff --git a/common/src/test/java/org/dromara/hertzbeat/common/util/GsonUtilTest.java b/common/src/test/java/org/dromara/hertzbeat/common/util/JsonUtilTest.java similarity index 85% rename from common/src/test/java/org/dromara/hertzbeat/common/util/GsonUtilTest.java rename to common/src/test/java/org/dromara/hertzbeat/common/util/JsonUtilTest.java index 3cb12bc05f0..8719b73de79 100644 --- a/common/src/test/java/org/dromara/hertzbeat/common/util/GsonUtilTest.java +++ b/common/src/test/java/org/dromara/hertzbeat/common/util/JsonUtilTest.java @@ -4,9 +4,9 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link GsonUtil} + * Test case for {@link JsonUtil} */ -class GsonUtilTest { +class JsonUtilTest { @BeforeEach void setUp() { @@ -27,4 +27,4 @@ void testFromJson() { @Test void testFromJson1() { } -} \ No newline at end of file +} diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/JobSchedulerInit.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/JobSchedulerInit.java index 98cf253d360..ca0af588255 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/JobSchedulerInit.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/JobSchedulerInit.java @@ -22,7 +22,7 @@ import org.dromara.hertzbeat.common.entity.job.Job; import org.dromara.hertzbeat.common.entity.manager.Monitor; import org.dromara.hertzbeat.common.entity.manager.Param; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.dao.MonitorDao; import org.dromara.hertzbeat.manager.dao.ParamDao; import lombok.extern.slf4j.Slf4j; @@ -66,7 +66,7 @@ public void run(String... args) throws Exception { // 构造采集任务Job实体 Job appDefine = appService.getAppDefine(monitor.getApp()); // todo 这里暂时是深拷贝处理 - appDefine = GsonUtil.fromJson(GsonUtil.toJson(appDefine), Job.class); + appDefine = JsonUtil.fromJson(JsonUtil.toJson(appDefine), Job.class); appDefine.setId(monitor.getJobId()); appDefine.setMonitorId(monitor.getId()); appDefine.setInterval(monitor.getIntervals()); diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 6176f9fe43a..a7a08038a25 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -46,7 +46,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; -import org.springframework.util.InvalidMimeTypeException; import java.time.LocalDateTime; import java.util.*; @@ -346,7 +345,7 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu break; case "key-value": try{ - GsonUtil.toJson(param.getValue()); + JsonUtil.toJson(param.getValue()); } catch (Exception e){ throw new IllegalArgumentException("Params field " + field + " value " + param.getValue() + " is invalid key-value value"); diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/AccountControllerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/AccountControllerTest.java index bbce732e6be..6df3d8c08b7 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/AccountControllerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/AccountControllerTest.java @@ -1,7 +1,7 @@ package org.dromara.hertzbeat.manager.controller; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.pojo.dto.LoginDto; import com.usthe.sureness.util.JsonWebTokenUtil; import org.junit.jupiter.api.BeforeEach; @@ -45,7 +45,7 @@ void authGetToken() throws Exception { .build(); this.mockMvc.perform(MockMvcRequestBuilders.post("/api/account/auth/form") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(loginDto))) + .content(JsonUtil.toJson(loginDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.data.token").exists()) @@ -53,7 +53,7 @@ void authGetToken() throws Exception { loginDto.setCredential("wrong_credential"); this.mockMvc.perform(MockMvcRequestBuilders.post("/api/account/auth/form") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(loginDto))) + .content(JsonUtil.toJson(loginDto))) .andExpect(jsonPath("$.code").value((int) CommonConstants.MONITOR_LOGIN_FAILED_CODE)) .andReturn(); } @@ -65,4 +65,4 @@ void refreshToken() throws Exception { .andExpect(jsonPath("$.code").value((int) CommonConstants.MONITOR_LOGIN_FAILED_CODE)) .andReturn(); } -} \ No newline at end of file +} diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorControllerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorControllerTest.java index 01edd1d9d4d..2e575622cda 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorControllerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorControllerTest.java @@ -3,7 +3,7 @@ import org.dromara.hertzbeat.common.entity.manager.Monitor; import org.dromara.hertzbeat.common.entity.manager.Param; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.pojo.dto.MonitorDto; import org.dromara.hertzbeat.manager.service.impl.MonitorServiceImpl; import org.junit.jupiter.api.BeforeEach; @@ -78,7 +78,7 @@ void addNewMonitor() throws Exception { MonitorDto monitorDto = DataTest(); this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -91,7 +91,7 @@ void modifyMonitor() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.put("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -155,7 +155,7 @@ void detectMonitor() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitor/detect") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Detect success.")) @@ -169,7 +169,7 @@ void addNewMonitorOptionalMetrics() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post("/api/monitor/optional") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Add success")) @@ -194,4 +194,4 @@ void getMonitorMetrics() throws Exception { .andExpect(status().isOk()) .andReturn(); } -} \ No newline at end of file +} diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorsControllerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorsControllerTest.java index ed60d9a5db0..8dc73520dc1 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorsControllerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/MonitorsControllerTest.java @@ -1,7 +1,7 @@ package org.dromara.hertzbeat.manager.controller; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.service.impl.MonitorServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -69,7 +69,7 @@ void deleteMonitors() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/monitors") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(ids))) + .content(JsonUtil.toJson(ids))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -82,7 +82,7 @@ void cancelManageMonitors() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/monitors/manage") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(ids))) + .content(JsonUtil.toJson(ids))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); @@ -95,7 +95,7 @@ void enableManageMonitors() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.get("/api/monitors/manage") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(ids))) + .content(JsonUtil.toJson(ids))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andReturn(); diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/NoticeConfigControllerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/NoticeConfigControllerTest.java index 914aae98713..a23ed28b98a 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/NoticeConfigControllerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/NoticeConfigControllerTest.java @@ -3,7 +3,7 @@ import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; import org.dromara.hertzbeat.common.entity.manager.NoticeRule; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.service.impl.NoticeConfigServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -81,7 +81,7 @@ void addNewNoticeReceiver() throws Exception { System.out.println(noticeReceiver); this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) + .content(JsonUtil.toJson(noticeReceiver))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Add success")) @@ -94,7 +94,7 @@ void editNoticeReceiver() throws Exception { System.out.println(noticeReceiver); this.mockMvc.perform(MockMvcRequestBuilders.put("/api/notice/receiver") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) + .content(JsonUtil.toJson(noticeReceiver))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Edit success")) @@ -141,7 +141,7 @@ void addNewNoticeRule() throws Exception { NoticeRule noticeRule = getNoticeRule(); this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/rule") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeRule))) + .content(JsonUtil.toJson(noticeRule))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Add success")) @@ -153,7 +153,7 @@ void editNoticeRule() throws Exception { NoticeRule noticeRule = getNoticeRule(); this.mockMvc.perform(MockMvcRequestBuilders.put("/api/notice/rule") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeRule))) + .content(JsonUtil.toJson(noticeRule))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Edit success")) @@ -205,7 +205,7 @@ void sendTestMsg() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver/send-test-msg") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) + .content(JsonUtil.toJson(noticeReceiver))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.FAIL_CODE)) .andExpect(jsonPath("$.msg").value("Notify service not available, please check config!")) @@ -217,10 +217,10 @@ void sendTestMsg() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post("/api/notice/receiver/send-test-msg") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(noticeReceiver))) + .content(JsonUtil.toJson(noticeReceiver))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) // .andExpect(jsonPath("$.msg").value("Notify service not available, please check config!")) .andReturn(); } -} \ No newline at end of file +} diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/TagControllerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/TagControllerTest.java index 3255b354c99..e6e2019d129 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/controller/TagControllerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/controller/TagControllerTest.java @@ -2,7 +2,7 @@ import org.dromara.hertzbeat.common.entity.manager.Tag; import org.dromara.hertzbeat.common.util.CommonConstants; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.service.impl.TagServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -57,7 +57,7 @@ void addNewTags() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.post("/api/tag") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(tags))) + .content(JsonUtil.toJson(tags))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Add success")) @@ -79,7 +79,7 @@ void modifyMonitor() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.put("/api/tag") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(tag))) + .content(JsonUtil.toJson(tag))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Modify success")) @@ -102,7 +102,7 @@ void deleteTags() throws Exception { this.mockMvc.perform(MockMvcRequestBuilders.delete("/api/tag") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(ids))) + .content(JsonUtil.toJson(ids))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) .andExpect(jsonPath("$.msg").value("Delete success")) @@ -110,4 +110,4 @@ void deleteTags() throws Exception { } -} \ No newline at end of file +} diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/dao/ParamDefineDaoTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/dao/ParamDefineDaoTest.java index 68e2734787b..85b9a0e299a 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/dao/ParamDefineDaoTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/dao/ParamDefineDaoTest.java @@ -43,8 +43,6 @@ void setUp() { .placeholder("mock placeholder") .creator("mock creator") .modifier("mock modifier") - .gmtCreate(LocalDateTime.now()) - .gmtUpdate(LocalDateTime.now()) .build(); paramDefine = paramDefineDao.saveAndFlush(paramDefine); @@ -62,4 +60,4 @@ void findParamDefinesByApp() { assertNotNull(paramDefineList); assertEquals(1, paramDefineList.size()); } -} \ No newline at end of file +} diff --git a/manager/src/test/java/org/dromara/hertzbeat/manager/support/GlobalExceptionHandlerTest.java b/manager/src/test/java/org/dromara/hertzbeat/manager/support/GlobalExceptionHandlerTest.java index beb16df4c18..21806478873 100644 --- a/manager/src/test/java/org/dromara/hertzbeat/manager/support/GlobalExceptionHandlerTest.java +++ b/manager/src/test/java/org/dromara/hertzbeat/manager/support/GlobalExceptionHandlerTest.java @@ -1,7 +1,7 @@ package org.dromara.hertzbeat.manager.support; import org.dromara.hertzbeat.common.entity.manager.Monitor; -import org.dromara.hertzbeat.common.util.GsonUtil; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.dromara.hertzbeat.manager.controller.MonitorController; import org.dromara.hertzbeat.manager.pojo.dto.MonitorDto; import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException; @@ -68,7 +68,7 @@ void handleMonitorDetectException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.code").value((int) DETECT_FAILED_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -97,7 +97,7 @@ void handleMonitorDatabaseException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) MONITOR_CONFLICT_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -126,7 +126,7 @@ void handleIllegalArgumentException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) PARAM_INVALID_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -155,7 +155,7 @@ void handleAlertNoticeException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) FAIL_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -184,7 +184,7 @@ void handleHttpMessageNotReadableException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.code").value((int) PARAM_INVALID_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -212,7 +212,7 @@ void handleInputValidException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.code").value((int) PARAM_INVALID_CODE)) .andExpect(jsonPath("$.msg").value("Min.monitorDto.monitor.intervals:must be greater than or equal to 10")); @@ -241,7 +241,7 @@ void handleDataAccessException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isConflict()) .andExpect(jsonPath("$.code").value((int) MONITOR_CONFLICT_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -281,7 +281,7 @@ void handleUnknownException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isConflict()) .andExpect(jsonPath("$.code").value((int) MONITOR_CONFLICT_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -310,7 +310,7 @@ void handleMonitorMetricsException() throws Exception { .build() .perform(MockMvcRequestBuilders.post("/api/monitor") .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtil.toJson(monitorDto))) + .content(JsonUtil.toJson(monitorDto))) .andExpect(status().isOk()) .andExpect(jsonPath("$.code").value((int) PARAM_INVALID_CODE)) .andExpect(jsonPath("$.msg").value("mock exception")); @@ -329,4 +329,4 @@ public UnknownException(String msg) { } } -} \ No newline at end of file +}