Commit 50efe02c by HlQ

[add]

1.添加 agent 删除接口
2.添加 redis 队列数据数量字段
3.服务新增编辑删除添加下发 mqtt server 的逻辑
[fix] 关注 mall 列表返回事件记录 bug
1 parent 6d98502c
...@@ -4,10 +4,20 @@ import lombok.Getter; ...@@ -4,10 +4,20 @@ import lombok.Getter;
import java.util.Arrays; import java.util.Arrays;
/**
* mqtt 消息枚举
*/
@Getter @Getter
public enum MqttMessageType { public enum MqttMessageType {
/** /**
* 服务列表增删改下发
*/
SERVICE_ADD("add"),
SERVICE_UPDATE("update"),
SERVICE_REMOVE("remove"),
SERVICE_ASSIGN("assign"),
/**
* 注册 * 注册
*/ */
REGISTER("REGISTER"), REGISTER("REGISTER"),
......
...@@ -44,6 +44,12 @@ public class AgentController { ...@@ -44,6 +44,12 @@ public class AgentController {
return agentService.update(dto); return agentService.update(dto);
} }
@DeleteMapping("/{id}")
@SaCheckPermission(value = "agent:remove", orRole = "admin")
public String remove(@PathVariable Long id) {
return agentService.removeById(id) ? "删除成功" : "删除失败";
}
@PostMapping("/upgrade/{uid}") @PostMapping("/upgrade/{uid}")
@SaCheckPermission(value = "agent:upgrade", orRole = "admin") @SaCheckPermission(value = "agent:upgrade", orRole = "admin")
public String upgradeCommand(@PathVariable String uid, Long upgradeId) { public String upgradeCommand(@PathVariable String uid, Long upgradeId) {
......
...@@ -39,65 +39,36 @@ public class MqttClientMessageListener { ...@@ -39,65 +39,36 @@ public class MqttClientMessageListener {
JsonNode jsonObj = JsonUtil.parseTree(payload); JsonNode jsonObj = JsonUtil.parseTree(payload);
String type = jsonObj.path("eventType").asText(); String type = jsonObj.path("eventType").asText();
switch (MqttMessageType.getEnumByType(type)) { switch (MqttMessageType.getEnumByType(type)) {
case MqttMessageType.REGISTER: case REGISTER -> {
String agentUid = jsonObj.path("agentUid").asText(); String agentUid = jsonObj.path("agentUid").asText();
updateTaskByAgent(agentUid); updateTaskByAgent(agentUid);
break; }
case MqttMessageType.PASSENGER_FLOW_INTERRUPT: // region mall + store 共有指标
handlePassengerFlowInterrupt(jsonObj.toString()); case PASSENGER_FLOW_INTERRUPT -> handlePassengerFlowInterrupt(jsonObj.toString());
break; case DEVICE_OFFLINE -> handleDeviceOffline(jsonObj.toString());
case MqttMessageType.DEVICE_OFFLINE: case REID_ANALYZE -> handleReid(jsonObj.toString());
handleDeviceOffline(jsonObj.toString()); case DEVICE_REGISTRATION -> handleDeviceRegistration(jsonObj.toString());
break; case HEADCOUNT_RATIO -> handleHeadcountRatio(jsonObj.toString());
case MqttMessageType.REID_ANALYZE: case STAFF_RECOGNIZE -> handleStaffRecognize(jsonObj.toString());
handleReid(jsonObj.toString());
break;
case HEADCOUNT_RATIO:
handleHeadcountRatio(jsonObj.toString());
break;
case STAFF_RECOGNIZE:
handleStaffRecognize(jsonObj.toString());
break;
// region store 事件
case STORE_CUSTOMER_UNDULATE:
handleStoreCustomerUndulate(jsonObj.toString());
break;
case STORE_GATE_DATA_UNDULATE:
handleStoreGateDateUndulate(jsonObj.toString());
break;
case STORE_INOUT_MATCH_RATIO:
handleStoreInoutMatchRatio(jsonObj.toString());
break;
case STORE_SINGLE_CLUSTER:
handleStoreSingleCluster(jsonObj.toString());
break;
case STORE_ENTER_RATIO:
handleStoreEnterRatio(jsonObj.toString());
break;
// endregion // endregion
// region mall 事件
case MALL_INOUT_DIFF: // region store 指标
handleMallInoutDiff(jsonObj.toString()); case STORE_CUSTOMER_UNDULATE -> handleStoreCustomerUndulate(jsonObj.toString());
break; case STORE_GATE_DATA_UNDULATE -> handleStoreGateDateUndulate(jsonObj.toString());
case MALL_SHOP_INOUT_DIFF: case STORE_INOUT_MATCH_RATIO -> handleStoreInoutMatchRatio(jsonObj.toString());
handleMallShopInoutDiff(jsonObj.toString()); case STORE_SINGLE_CLUSTER -> handleStoreSingleCluster(jsonObj.toString());
break; case STORE_ENTER_RATIO -> handleStoreEnterRatio(jsonObj.toString());
case MALL_DATA_UNDULATE:
handleMallDataUndulate(jsonObj.toString());
break;
case MALL_GATE_DATA_UNDULATE:
handleMallGateDataUndulate(jsonObj.toString());
break;
case MALL_SHOP_DATA_UNDULATE:
handleMallShopDataUndulate(jsonObj.toString());
break;
case MALL_DILATATION_UNDULATE:
handleMallDilatationUndulate(jsonObj.toString());
break;
// endregion // endregion
default:
log.info("未定义的消息类型:{}, payload:{}", type, jsonObj); // region mall 指标
break; case MALL_INOUT_DIFF -> handleMallInoutDiff(jsonObj.toString());
case MALL_SHOP_INOUT_DIFF -> handleMallShopInoutDiff(jsonObj.toString());
case MALL_DATA_UNDULATE -> handleMallDataUndulate(jsonObj.toString());
case MALL_GATE_DATA_UNDULATE -> handleMallGateDataUndulate(jsonObj.toString());
case MALL_SHOP_DATA_UNDULATE -> handleMallShopDataUndulate(jsonObj.toString());
case MALL_DILATATION_UNDULATE -> handleMallDilatationUndulate(jsonObj.toString());
// endregion
default -> log.info("未定义的消息类型:{}, payload:{}", type, jsonObj);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("解析数据异常", e); log.error("解析数据异常", e);
...@@ -114,7 +85,7 @@ public class MqttClientMessageListener { ...@@ -114,7 +85,7 @@ public class MqttClientMessageListener {
.eq(RAgentEvent::getAgentUid, agentUid) .eq(RAgentEvent::getAgentUid, agentUid)
.eq(RAgentEvent::getControlSwitch, 1).list(); .eq(RAgentEvent::getControlSwitch, 1).list();
if (CollUtil.isEmpty(list)) { if (CollUtil.isEmpty(list)) {
log.error("agent:{} 未配置事件监测", agentUid); log.info("agent:{} 未配置事件监测", agentUid);
return; return;
} }
var topic = TopicUtil.getEventTopic(agentUid); var topic = TopicUtil.getEventTopic(agentUid);
...@@ -150,6 +121,16 @@ public class MqttClientMessageListener { ...@@ -150,6 +121,16 @@ public class MqttClientMessageListener {
} }
/** /**
* 设备注册次数异常
*
* @param payloadStr 事件记录
*/
private void handleDeviceRegistration(String payloadStr) {
EventRecord eventRecord = JsonUtil.parseObject(payloadStr, EventRecord.class);
recordService.save(eventRecord);
}
/**
* 人数/人次异常 * 人数/人次异常
* *
* @param payloadStr 事件记录 * @param payloadStr 事件记录
......
...@@ -48,6 +48,12 @@ public class AgentRecord { ...@@ -48,6 +48,12 @@ public class AgentRecord {
private Object networkInfo; private Object networkInfo;
/** /**
* redis 队列数据数量
*/
@TableField(value = "queue_data", typeHandler = JsonbTypeHandler.class)
private Object queueData;
/**
* 启动时间 * 启动时间
*/ */
@TableField(value = "boot_time") @TableField(value = "boot_time")
......
...@@ -21,6 +21,7 @@ import org.dromara.hutool.core.util.RandomUtil; ...@@ -21,6 +21,7 @@ import org.dromara.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import vion.constant.MqttMessageType;
import vion.dto.monitor.AgentDTO; import vion.dto.monitor.AgentDTO;
import vion.dto.monitor.MqttAuthDTO; import vion.dto.monitor.MqttAuthDTO;
import vion.dto.monitor.OrgDTO; import vion.dto.monitor.OrgDTO;
...@@ -180,17 +181,52 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp ...@@ -180,17 +181,52 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp
return rAgentService; return rAgentService;
}).toList(); }).toList();
rAgentServiceService.saveBatch(rAgentServiceList); rAgentServiceService.saveBatch(rAgentServiceList);
var topic = TopicUtil.getServiceTopic(uid);
if (StrUtil.isBlank(topic)) {
var msg = StrUtil.format("agent:{} 获取topic失败", uid);
log.error(msg);
return msg;
} }
return "更新成功"; var objNode = JsonUtil.createObj()
.put("type", MqttMessageType.SERVICE_ADD.getType())
.putPOJO("services", JsonUtil.toJsonString(rAgentServiceList));
client.publish(topic, JsonUtil.toJsonByte(objNode), MqttQoS.QOS2);
}
return "保存成功";
} }
@Override @Override
public String updateAgent2ServiceById(Long id, RAgentService rAgentService) { public String updateAgent2ServiceById(Long id, RAgentService rAgentService) {
return rAgentServiceService.updateById(rAgentService) ? "更新成功" : "更新失败"; if (rAgentServiceService.updateById(rAgentService)) {
var agentService = rAgentServiceService.getById(id);
var topic = TopicUtil.getServiceTopic(agentService.getAgentUid());
if (StrUtil.isBlank(topic)) {
var msg = StrUtil.format("agent:{} 获取topic失败", agentService.getAgentUid());
log.error(msg);
return msg;
}
var objNode = JsonUtil.createObj()
.put("type", MqttMessageType.SERVICE_UPDATE.getType())
.putPOJO("services", JsonUtil.toJsonString(agentService));
client.publish(topic, JsonUtil.toJsonByte(objNode), MqttQoS.QOS2);
return "更新成功";
}
return "更新失败";
} }
@Override @Override
public String removeAgent2ServiceById(Long id) { public String removeAgent2ServiceById(Long id) {
var agentService = rAgentServiceService.getById(id);
var topic = TopicUtil.getServiceTopic(agentService.getAgentUid());
if (StrUtil.isBlank(topic)) {
var msg = StrUtil.format("agent:{} 获取topic失败", agentService.getAgentUid());
log.error(msg);
return msg;
}
var objNode = JsonUtil.createObj()
.put("type", MqttMessageType.SERVICE_REMOVE.getType())
.putPOJO("services", JsonUtil.toJsonString(agentService));
client.publish(topic, JsonUtil.toJsonByte(objNode), MqttQoS.QOS2);
return rAgentServiceService.removeById(id) ? "删除成功 : " : "删除失败"; return rAgentServiceService.removeById(id) ? "删除成功 : " : "删除失败";
} }
...@@ -202,7 +238,10 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp ...@@ -202,7 +238,10 @@ public class AgentServiceImpl extends MPJBaseServiceImpl<AgentMapper, Agent> imp
log.error(msg); log.error(msg);
return msg; return msg;
} }
return client.publish(topic, JsonUtil.toJsonByte(serviceInfoList), MqttQoS.QOS2) ? "服务列表下发成功" : var objNode = JsonUtil.createObj()
.put("type", MqttMessageType.SERVICE_ASSIGN.getType())
.putPOJO("services", JsonUtil.toJsonString(serviceInfoList));
return client.publish(topic, JsonUtil.toJsonByte(objNode), MqttQoS.QOS2) ? "服务列表下发成功" :
"服务列表下发失败"; "服务列表下发失败";
} }
......
...@@ -52,9 +52,9 @@ public class EventServiceImpl extends MPJBaseServiceImpl<EventMapper, Event> imp ...@@ -52,9 +52,9 @@ public class EventServiceImpl extends MPJBaseServiceImpl<EventMapper, Event> imp
.orderByDesc("mall_uid", "create_time"); .orderByDesc("mall_uid", "create_time");
var eventRecordList1 = eventRecordService.selectJoinList(EventRecord.class, eventRecWrapper1);*/ var eventRecordList1 = eventRecordService.selectJoinList(EventRecord.class, eventRecWrapper1);*/
var eventRecWrapper = Wrappers.<EventRecord>query() var eventRecWrapper = Wrappers.<EventRecord>query()
.select("DISTINCT on (mall_uid) *") .select("DISTINCT on (mall_uid,event_type) *")
.eq("mall_uid", dto.getMallUid()) .eq("mall_uid", dto.getMallUid())
.orderByDesc("mall_uid", "create_time"); .orderByDesc("mall_uid", "event_type", "create_time");
var eventRecordList = eventRecordService.list(eventRecWrapper); var eventRecordList = eventRecordService.list(eventRecWrapper);
var eventUid2SelfMap = eventRecordList.stream().collect(Collectors.toMap(EventRecord::getEventUid, Function.identity())); var eventUid2SelfMap = eventRecordList.stream().collect(Collectors.toMap(EventRecord::getEventUid, Function.identity()));
......
...@@ -87,9 +87,9 @@ public class MallServiceImpl extends MPJBaseServiceImpl<MallMapper, Mall> implem ...@@ -87,9 +87,9 @@ public class MallServiceImpl extends MPJBaseServiceImpl<MallMapper, Mall> implem
var mallUidList = r.stream().map(MallVO::getUid).toList(); var mallUidList = r.stream().map(MallVO::getUid).toList();
// fixme {@link EventServiceImpl #51} // fixme {@link EventServiceImpl #51}
var eventRecWrapper = Wrappers.<EventRecord>query() var eventRecWrapper = Wrappers.<EventRecord>query()
.select("DISTINCT on (mall_uid) *") .select("DISTINCT on (mall_uid,event_type) *")
.in("mall_uid", mallUidList) .in("mall_uid", mallUidList)
.orderByDesc("mall_uid", "create_time"); .orderByDesc("mall_uid", "event_type", "create_time");
var eventRecordList = eventRecordService.list(eventRecWrapper); var eventRecordList = eventRecordService.list(eventRecWrapper);
var mallUid2RecMap = eventRecordList.stream().collect(Collectors.groupingBy(EventRecord::getMallUid)); var mallUid2RecMap = eventRecordList.stream().collect(Collectors.groupingBy(EventRecord::getMallUid));
...@@ -107,15 +107,14 @@ public class MallServiceImpl extends MPJBaseServiceImpl<MallMapper, Mall> implem ...@@ -107,15 +107,14 @@ public class MallServiceImpl extends MPJBaseServiceImpl<MallMapper, Mall> implem
@Override @Override
public Page<MallVO> listErrorAttention(MallDTO dto) { public Page<MallVO> listErrorAttention(MallDTO dto) {
var eventRecWrapper = Wrappers.<EventRecord>query() var eventRecWrapper = Wrappers.<EventRecord>query()
.select("DISTINCT on (mall_uid) *") .select("DISTINCT on (mall_uid,event_type) *")
.eq("agent_type", dto.getAgentType()) .eq("agent_type", dto.getAgentType())
.ne("status", 1) .orderByDesc("mall_uid", "event_type", "create_time");
.orderByDesc("mall_uid", "create_time");
var eventRecordList = eventRecordService.list(eventRecWrapper); var eventRecordList = eventRecordService.list(eventRecWrapper);
if (CollUtil.isEmpty(eventRecordList)) { var mallUidList = eventRecordList.stream().filter(r -> r.getStatus() != 1).map(EventRecord::getMallUid).toList();
return null; if (CollUtil.isEmpty(mallUidList)) {
return new Page<>();
} }
var mallUidList = eventRecordList.stream().map(EventRecord::getMallUid).toList();
var agentEventList = agentEventService.lambdaQuery().in(RAgentEvent::getMallUid, mallUidList).list(); var agentEventList = agentEventService.lambdaQuery().in(RAgentEvent::getMallUid, mallUidList).list();
var mallUid2AgentEventMap = agentEventList.stream().collect(Collectors.groupingBy(RAgentEvent::getMallUid)); var mallUid2AgentEventMap = agentEventList.stream().collect(Collectors.groupingBy(RAgentEvent::getMallUid));
......
package vion.utils; package vion.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
...@@ -28,6 +32,7 @@ import java.time.LocalTime; ...@@ -28,6 +32,7 @@ import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
/** /**
* JSON 工具类 * JSON 工具类
...@@ -37,59 +42,27 @@ import java.util.List; ...@@ -37,59 +42,27 @@ import java.util.List;
@Slf4j @Slf4j
public class JsonUtil { public class JsonUtil {
private static ObjectMapper objectMapper = new ObjectMapper();
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormat = "yyyy-MM-dd";
private static final String timeFormat = "HH:mm:ss";
static {
var module = new JavaTimeModule();
module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
objectMapper.registerModule(module);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 忽略 null 值
// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
/**
* 初始化 objectMapper 属性
* <p>
* 通过这样的方式,使用 Spring 创建的 ObjectMapper Bean
*
* @param objectMapper ObjectMapper 对象
*/
public static void init(ObjectMapper objectMapper) {
JsonUtil.objectMapper = objectMapper;
}
public static ObjectNode createObj() { public static ObjectNode createObj() {
return objectMapper.createObjectNode(); return getObjectMapper().createObjectNode();
} }
public static ArrayNode createArr() { public static ArrayNode createArr() {
return objectMapper.createArrayNode(); return getObjectMapper().createArrayNode();
} }
@SneakyThrows @SneakyThrows
public static String toJsonString(Object object) { public static String toJsonString(Object object) {
return objectMapper.writeValueAsString(object); return getObjectMapper().writeValueAsString(object);
} }
@SneakyThrows @SneakyThrows
public static byte[] toJsonByte(Object object) { public static byte[] toJsonByte(Object object) {
return objectMapper.writeValueAsBytes(object); return getObjectMapper().writeValueAsBytes(object);
} }
@SneakyThrows @SneakyThrows
public static String toJsonPrettyString(Object object) { public static String toJsonPrettyString(Object object) {
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
} }
public static <T> T parseObject(String text, Class<T> clazz) { public static <T> T parseObject(String text, Class<T> clazz) {
...@@ -97,7 +70,7 @@ public class JsonUtil { ...@@ -97,7 +70,7 @@ public class JsonUtil {
return null; return null;
} }
try { try {
return objectMapper.readValue(text, clazz); return getObjectMapper().readValue(text, clazz);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -109,9 +82,9 @@ public class JsonUtil { ...@@ -109,9 +82,9 @@ public class JsonUtil {
return null; return null;
} }
try { try {
JsonNode treeNode = objectMapper.readTree(text); JsonNode treeNode = getObjectMapper().readTree(text);
JsonNode pathNode = treeNode.path(path); JsonNode pathNode = treeNode.path(path);
return objectMapper.readValue(pathNode.toString(), clazz); return getObjectMapper().readValue(pathNode.toString(), clazz);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -123,7 +96,7 @@ public class JsonUtil { ...@@ -123,7 +96,7 @@ public class JsonUtil {
return null; return null;
} }
try { try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructType(type)); return getObjectMapper().readValue(text, getObjectMapper().getTypeFactory().constructType(type));
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -144,7 +117,7 @@ public class JsonUtil { ...@@ -144,7 +117,7 @@ public class JsonUtil {
return null; return null;
} }
try { try {
return objectMapper.readValue(text, clazz); return getObjectMapper().readValue(text, clazz);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -155,7 +128,7 @@ public class JsonUtil { ...@@ -155,7 +128,7 @@ public class JsonUtil {
return null; return null;
} }
try { try {
return objectMapper.readValue(bytes, clazz); return getObjectMapper().readValue(bytes, clazz);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", bytes, e); log.error("json parse err,json:{}", bytes, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -164,7 +137,7 @@ public class JsonUtil { ...@@ -164,7 +137,7 @@ public class JsonUtil {
public static <T> T parseObject(String text, TypeReference<T> typeReference) { public static <T> T parseObject(String text, TypeReference<T> typeReference) {
try { try {
return objectMapper.readValue(text, typeReference); return getObjectMapper().readValue(text, typeReference);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -180,7 +153,7 @@ public class JsonUtil { ...@@ -180,7 +153,7 @@ public class JsonUtil {
*/ */
public static <T> T parseObjectQuietly(String text, TypeReference<T> typeReference) { public static <T> T parseObjectQuietly(String text, TypeReference<T> typeReference) {
try { try {
return objectMapper.readValue(text, typeReference); return getObjectMapper().readValue(text, typeReference);
} catch (IOException e) { } catch (IOException e) {
return null; return null;
} }
...@@ -191,7 +164,7 @@ public class JsonUtil { ...@@ -191,7 +164,7 @@ public class JsonUtil {
return new ArrayList<>(); return new ArrayList<>();
} }
try { try {
return objectMapper.readValue(text, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); return getObjectMapper().readValue(text, getObjectMapper().getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -203,9 +176,9 @@ public class JsonUtil { ...@@ -203,9 +176,9 @@ public class JsonUtil {
return null; return null;
} }
try { try {
JsonNode treeNode = objectMapper.readTree(text); JsonNode treeNode = getObjectMapper().readTree(text);
JsonNode pathNode = treeNode.path(path); JsonNode pathNode = treeNode.path(path);
return objectMapper.readValue(pathNode.toString(), objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); return getObjectMapper().readValue(pathNode.toString(), getObjectMapper().getTypeFactory().constructCollectionType(List.class, clazz));
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -214,7 +187,7 @@ public class JsonUtil { ...@@ -214,7 +187,7 @@ public class JsonUtil {
public static JsonNode parseTree(String text) { public static JsonNode parseTree(String text) {
try { try {
return objectMapper.readTree(text); return getObjectMapper().readTree(text);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -223,7 +196,7 @@ public class JsonUtil { ...@@ -223,7 +196,7 @@ public class JsonUtil {
public static JsonNode parseTree(byte[] text) { public static JsonNode parseTree(byte[] text) {
try { try {
return objectMapper.readTree(text); return getObjectMapper().readTree(text);
} catch (IOException e) { } catch (IOException e) {
log.error("json parse err,json:{}", text, e); log.error("json parse err,json:{}", text, e);
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -232,12 +205,73 @@ public class JsonUtil { ...@@ -232,12 +205,73 @@ public class JsonUtil {
public static boolean isJson(String jsonString) { public static boolean isJson(String jsonString) {
try { try {
objectMapper.readTree(jsonString); getObjectMapper().readTree(jsonString);
return true; return true;
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
} }
/**
* 获取 ObjectMapper 实例
*
* @return ObjectMapper
*/
public static ObjectMapper getObjectMapper() {
return JacksonHolder.INSTANCE;
}
private static class JacksonHolder {
private static final ObjectMapper INSTANCE = new JacksonObjectMapper();
}
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormat = "yyyy-MM-dd";
private static final String timeFormat = "HH:mm:ss";
private static class JacksonObjectMapper extends ObjectMapper {
private static final long serialVersionUID = 4288193147502386170L;
JacksonObjectMapper() {
super(jsonFactory());
super.setLocale(Locale.CHINA);
super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 蛇形命名转换为驼峰命名
// super.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy());
super.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Long 转为 String 防止 js 丢失精度
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
super.registerModule(simpleModule);
var module = new JavaTimeModule();
module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
super.registerModule(module);
super.findAndRegisterModules();
}
JacksonObjectMapper(ObjectMapper src) {
super(src);
}
private static JsonFactory jsonFactory() {
return JsonFactory.builder()
// 可解析反斜杠引用的所有字符
.configure(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true)
// 允许JSON字符串包含非引号控制字符(值小于32的ASCII字符,包含制表符和换行符)
.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS, true)
.build();
}
@Override
public ObjectMapper copy() {
return new JacksonObjectMapper(this);
}
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!