fix:jsonutil修改

V0.5.x
xiwa 2023-06-18 22:23:04 +08:00
parent 195ab6761f
commit 2153e9956b
4 changed files with 12 additions and 11 deletions

View File

@ -20,7 +20,8 @@ import java.util.Objects;
*/ */
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonUtils { 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() { public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER; return OBJECT_MAPPER;

View File

@ -11,13 +11,13 @@ package cc.iotkit.test.mqtt.service;
import cc.iotkit.common.constant.Constants; import cc.iotkit.common.constant.Constants;
import cc.iotkit.common.utils.JsonUtils;
import cc.iotkit.test.mqtt.config.Mqtt; import cc.iotkit.test.mqtt.config.Mqtt;
import cc.iotkit.test.mqtt.model.Request; import cc.iotkit.test.mqtt.model.Request;
import io.netty.handler.codec.mqtt.MqttQoS; import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.AsyncResult; import io.vertx.core.AsyncResult;
import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.mqtt.MqttClient; import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions; import io.vertx.mqtt.MqttClientOptions;
import io.vertx.mqtt.messages.MqttConnAckMessage; import io.vertx.mqtt.messages.MqttConnAckMessage;
@ -64,7 +64,7 @@ public class Gateway extends Device {
return; return;
} }
if(isConnecting){ if (isConnecting) {
return; return;
} }
@ -114,7 +114,7 @@ public class Gateway extends Device {
request.setId(UUID.randomUUID().toString()); request.setId(UUID.randomUUID().toString());
request.setParams(subDevice); request.setParams(subDevice);
String registerTopic = String.format("/sys/%s/%s/s/register", productKey, deviceName); 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); client.publish(registerTopic, Buffer.buffer(payload), MqttQoS.AT_LEAST_ONCE, false, false);
log.info("publish message,topic:{},payload:{}", registerTopic, payload); log.info("publish message,topic:{},payload:{}", registerTopic, payload);
} }

View File

@ -10,12 +10,12 @@
package cc.iotkit.test.mqtt.service; 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.Request;
import cc.iotkit.test.mqtt.model.Response; import cc.iotkit.test.mqtt.model.Response;
import io.netty.handler.codec.mqtt.MqttQoS; import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.Handler; import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.mqtt.MqttClient; import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.messages.MqttPublishMessage; import io.vertx.mqtt.messages.MqttPublishMessage;
import lombok.Data; import lombok.Data;
@ -53,7 +53,7 @@ public class MessageHandler implements Handler<MqttPublishMessage> {
log.info("received msg,topic:{},payload:{}", topic, payload); log.info("received msg,topic:{},payload:{}", topic, payload);
if (topic.endsWith("register_reply")) { if (topic.endsWith("register_reply")) {
Response response = JsonUtils.parseObject(payload, Response.class); Response response = Json.decodeValue(payload, Response.class);
//子设备注册成功 //子设备注册成功
if (response.getCode() == 0) { if (response.getCode() == 0) {
Map<String, Object> data = response.getData(); Map<String, Object> data = response.getData();
@ -78,18 +78,18 @@ public class MessageHandler implements Handler<MqttPublishMessage> {
if (topic.endsWith("_reply")) { if (topic.endsWith("_reply")) {
return; return;
} }
Request request = JsonUtils.parseObject(payload, Request.class); Request request = Json.decodeValue(payload, Request.class);
Response response = new Response(request.getId(), 0, new HashMap<>()); Response response = new Response(request.getId(), 0, new HashMap<>());
client.publish(topic.replace("/c/", "/s/") + "_reply", 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"; String setTopic = "/c/service/property/set";
if (topic.endsWith(setTopic)) { if (topic.endsWith(setTopic)) {
request.setId(UUID.randomUUID().toString()); request.setId(UUID.randomUUID().toString());
client.publish(topic.replace(setTopic, "/s/event/property/post"), 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) { } catch (Throwable e) {
log.info("receive msg error", e); log.info("receive msg error", e);

View File

@ -9,10 +9,10 @@
*/ */
package cc.iotkit.test.mqtt.service; 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.Request;
import io.netty.handler.codec.mqtt.MqttQoS; import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.mqtt.MqttClient; import io.vertx.mqtt.MqttClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -51,7 +51,7 @@ public class ReportTask {
if (!client.isConnected()) { if (!client.isConnected()) {
return; return;
} }
String msg = JsonUtils.toJsonString(request); String msg = Json.encode(request);
log.info("send msg,topic:{},payload:{}", topic, msg); log.info("send msg,topic:{},payload:{}", topic, msg);
client.publish(topic, Buffer.buffer(msg), MqttQoS.AT_LEAST_ONCE, false, false); client.publish(topic, Buffer.buffer(msg), MqttQoS.AT_LEAST_ONCE, false, false);