8.10 修复日期序列号的bug

This commit is contained in:
zhangsan 2025-08-10 22:15:15 +08:00
parent d9963f0607
commit 97e4fe2763
3 changed files with 14 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import java.util.List;
* 图片上传模板 * 图片上传模板
*/ */
@Slf4j @Slf4j
@RequiredArgsConstructor
public abstract class PictureUploadTemplate { public abstract class PictureUploadTemplate {
@Resource @Resource

View File

@ -40,12 +40,19 @@ public class PictureEditHandler extends TextWebSocketHandler {
@Lazy @Lazy
private PictureEditEventProducer pictureEditEventProducer; private PictureEditEventProducer pictureEditEventProducer;
private final ObjectMapper objectMapper;
public PictureEditHandler(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
// 每张图片的编辑状态key: pictureId, value: 当前正在编辑的用户 ID // 每张图片的编辑状态key: pictureId, value: 当前正在编辑的用户 ID
private final Map<Long, Long> pictureEditingUsers = new ConcurrentHashMap<>(); private final Map<Long, Long> pictureEditingUsers = new ConcurrentHashMap<>();
// 保存所有连接的会话key: pictureId, value: 所有正在编辑这张图片的用户会话集合 // 保存所有连接的会话key: pictureId, value: 所有正在编辑这张图片的用户会话集合
private final Map<Long, Set<WebSocketSession>> pictureSessions = new ConcurrentHashMap<>(); private final Map<Long, Set<WebSocketSession>> pictureSessions = new ConcurrentHashMap<>();
/** /**
* 实现连接建立成功后执行的方法保存会话到集合中并且给其他会话发送消息 * 实现连接建立成功后执行的方法保存会话到集合中并且给其他会话发送消息
* *
@ -212,18 +219,10 @@ public class PictureEditHandler extends TextWebSocketHandler {
private void broadcastToPicture(Long pictureId, PictureEditResponseMessage pictureEditResponseMessage, WebSocketSession excludeSession) throws IOException { private void broadcastToPicture(Long pictureId, PictureEditResponseMessage pictureEditResponseMessage, WebSocketSession excludeSession) throws IOException {
Set<WebSocketSession> sessionSet = pictureSessions.get(pictureId); Set<WebSocketSession> sessionSet = pictureSessions.get(pictureId);
if (CollUtil.isNotEmpty(sessionSet)) { if (CollUtil.isNotEmpty(sessionSet)) {
// 创建 ObjectMapper // 使用全局注入的 objectMapper已经配置 JavaTimeModule LongString
ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(pictureEditResponseMessage);
// 配置序列化 Long 类型转为 String解决丢失精度问题 TextMessage textMessage = new TextMessage(json);
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance); // 支持 long 基本类型
objectMapper.registerModule(module);
// 序列化为 JSON 字符串
String message = objectMapper.writeValueAsString(pictureEditResponseMessage);
TextMessage textMessage = new TextMessage(message);
for (WebSocketSession session : sessionSet) { for (WebSocketSession session : sessionSet) {
// 排除掉的 session 不发送 比如自己发送的广播自己不接收
if (excludeSession != null && session.equals(excludeSession)) { if (excludeSession != null && session.equals(excludeSession)) {
continue; continue;
} }

View File

@ -4,22 +4,22 @@ import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor; import com.lmax.disruptor.dsl.Disruptor;
import edu.whut.smilepicturebackend.manager.websocket.model.PictureEditRequestMessage; import edu.whut.smilepicturebackend.manager.websocket.model.PictureEditRequestMessage;
import edu.whut.smilepicturebackend.model.entity.User; import edu.whut.smilepicturebackend.model.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import javax.annotation.Resource;
/** /**
* 图片编辑事件生产者 * 图片编辑事件生产者
*/ */
@Component @Component
@Slf4j @Slf4j
@RequiredArgsConstructor
public class PictureEditEventProducer { public class PictureEditEventProducer {
@Resource private final Disruptor<PictureEditEvent> pictureEditEventDisruptor;
private Disruptor<PictureEditEvent> pictureEditEventDisruptor;
/** /**
* 发布事件 * 发布事件