fix:jsonutil修改
parent
195ab6761f
commit
2153e9956b
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<MqttPublishMessage> {
|
|||
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<String, Object> data = response.getData();
|
||||
|
@ -78,18 +78,18 @@ public class MessageHandler implements Handler<MqttPublishMessage> {
|
|||
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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue