From 2153e9956b14576e9d912153994a6872f1a2ac57 Mon Sep 17 00:00:00 2001 From: xiwa Date: Sun, 18 Jun 2023 22:23:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:jsonutil=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cc/iotkit/common/utils/JsonUtils.java | 3 ++- .../main/java/cc/iotkit/test/mqtt/service/Gateway.java | 6 +++--- .../cc/iotkit/test/mqtt/service/MessageHandler.java | 10 +++++----- .../java/cc/iotkit/test/mqtt/service/ReportTask.java | 4 ++-- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/JsonUtils.java b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/JsonUtils.java index 94e3686d..4036ffda 100644 --- a/iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/JsonUtils.java +++ b/iot-common/iot-common-core/src/main/java/cc/iotkit/common/utils/JsonUtils.java @@ -20,7 +20,8 @@ import java.util.Objects; */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class JsonUtils { - private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); public static ObjectMapper getObjectMapper() { return OBJECT_MAPPER; diff --git a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/Gateway.java b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/Gateway.java index efaa8df3..9a34c233 100755 --- a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/Gateway.java +++ b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/Gateway.java @@ -11,13 +11,13 @@ package cc.iotkit.test.mqtt.service; import cc.iotkit.common.constant.Constants; -import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.test.mqtt.config.Mqtt; import cc.iotkit.test.mqtt.model.Request; import io.netty.handler.codec.mqtt.MqttQoS; import io.vertx.core.AsyncResult; import io.vertx.core.Handler; import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.Json; import io.vertx.mqtt.MqttClient; import io.vertx.mqtt.MqttClientOptions; import io.vertx.mqtt.messages.MqttConnAckMessage; @@ -64,7 +64,7 @@ public class Gateway extends Device { return; } - if(isConnecting){ + if (isConnecting) { return; } @@ -114,7 +114,7 @@ public class Gateway extends Device { request.setId(UUID.randomUUID().toString()); request.setParams(subDevice); String registerTopic = String.format("/sys/%s/%s/s/register", productKey, deviceName); - String payload = JsonUtils.toJsonString(request); + String payload = Json.encode(request); client.publish(registerTopic, Buffer.buffer(payload), MqttQoS.AT_LEAST_ONCE, false, false); log.info("publish message,topic:{},payload:{}", registerTopic, payload); } diff --git a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/MessageHandler.java b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/MessageHandler.java index 43acf58a..471257ae 100755 --- a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/MessageHandler.java +++ b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/MessageHandler.java @@ -10,12 +10,12 @@ package cc.iotkit.test.mqtt.service; -import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.test.mqtt.model.Request; import cc.iotkit.test.mqtt.model.Response; import io.netty.handler.codec.mqtt.MqttQoS; import io.vertx.core.Handler; import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.Json; import io.vertx.mqtt.MqttClient; import io.vertx.mqtt.messages.MqttPublishMessage; import lombok.Data; @@ -53,7 +53,7 @@ public class MessageHandler implements Handler { log.info("received msg,topic:{},payload:{}", topic, payload); if (topic.endsWith("register_reply")) { - Response response = JsonUtils.parseObject(payload, Response.class); + Response response = Json.decodeValue(payload, Response.class); //子设备注册成功 if (response.getCode() == 0) { Map data = response.getData(); @@ -78,18 +78,18 @@ public class MessageHandler implements Handler { if (topic.endsWith("_reply")) { return; } - Request request = JsonUtils.parseObject(payload, Request.class); + Request request = Json.decodeValue(payload, Request.class); Response response = new Response(request.getId(), 0, new HashMap<>()); client.publish(topic.replace("/c/", "/s/") + "_reply", - Buffer.buffer(JsonUtils.toJsonString(response)), MqttQoS.AT_LEAST_ONCE, false, false); + Buffer.buffer(Json.encode(response)), MqttQoS.AT_LEAST_ONCE, false, false); //属性设置后上报属性 String setTopic = "/c/service/property/set"; if (topic.endsWith(setTopic)) { request.setId(UUID.randomUUID().toString()); client.publish(topic.replace(setTopic, "/s/event/property/post"), - Buffer.buffer(JsonUtils.toJsonString(request)), MqttQoS.AT_LEAST_ONCE, false, false); + Buffer.buffer(Json.encode(request)), MqttQoS.AT_LEAST_ONCE, false, false); } } catch (Throwable e) { log.info("receive msg error", e); diff --git a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/ReportTask.java b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/ReportTask.java index 3de4cb22..063d50b9 100755 --- a/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/ReportTask.java +++ b/iot-test-tool/iot-test-mqtt/src/main/java/cc/iotkit/test/mqtt/service/ReportTask.java @@ -9,10 +9,10 @@ */ package cc.iotkit.test.mqtt.service; -import cc.iotkit.common.utils.JsonUtils; import cc.iotkit.test.mqtt.model.Request; import io.netty.handler.codec.mqtt.MqttQoS; import io.vertx.core.buffer.Buffer; +import io.vertx.core.json.Json; import io.vertx.mqtt.MqttClient; import lombok.extern.slf4j.Slf4j; @@ -51,7 +51,7 @@ public class ReportTask { if (!client.isConnected()) { return; } - String msg = JsonUtils.toJsonString(request); + String msg = Json.encode(request); log.info("send msg,topic:{},payload:{}", topic, msg); client.publish(topic, Buffer.buffer(msg), MqttQoS.AT_LEAST_ONCE, false, false);