diff --git a/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java b/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java index 77da8ec..2081390 100644 --- a/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java +++ b/src/main/java/com/SafeNet/Backend/domain/message/controller/MessageController.java @@ -15,6 +15,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; // 로깅을 위해 추가 import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.security.access.AccessDeniedException; @@ -59,10 +60,16 @@ public void message(MessageDto messageDto) { @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content(mediaType = "application/json")) }) @Parameter(name = "roomId", description = "메시지를 조회할 채팅방의 ID", required = true, example = "123") - public ResponseEntity> loadMessage( + public ResponseEntity loadMessage( @RequestHeader(name = "ACCESS_TOKEN", required = false) String accessToken, @RequestHeader(name = "REFRESH_TOKEN", required = false) String refreshToken, @PathVariable String roomId) { - return ResponseEntity.ok(messageService.loadMessage(roomId)); + try { + List messages = messageService.loadMessage(roomId); + return ResponseEntity.ok(messages); + } catch (Exception e) { + log.error("Failed to load messages for room {}: {}", roomId, e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to load messages: " + e.getMessage()); + } } } diff --git a/src/main/java/com/SafeNet/Backend/domain/message/service/MessageService.java b/src/main/java/com/SafeNet/Backend/domain/message/service/MessageService.java index 72d7cd9..013a13a 100644 --- a/src/main/java/com/SafeNet/Backend/domain/message/service/MessageService.java +++ b/src/main/java/com/SafeNet/Backend/domain/message/service/MessageService.java @@ -31,6 +31,7 @@ public MessageService( this.messageRepository = messageRepository; this.messageRoomRepository = messageRoomRepository; } + public void saveMessage(MessageDto messageDto) { MessageRoom messageRoom = messageRoomRepository.findByRoomId(messageDto.getRoomId()) .orElseThrow(() -> new IllegalArgumentException("해당 쪽지방이 존재하지 않습니다.")); @@ -49,7 +50,7 @@ public void saveMessage(MessageDto messageDto) { redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(MessageDto.class)); redisTemplateMessage.opsForList().rightPush(messageDto.getRoomId(), messageDto); - redisTemplateMessage.expire(messageDto.getRoomId(), 1, TimeUnit.HOURS); // redis에서 1시간마다 삭제 + redisTemplateMessage.expire(messageDto.getRoomId(), 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제 } /** @@ -78,7 +79,8 @@ public List loadMessage(String roomId) { .build(); messageList.add(messageDto); redisTemplateMessage.setValueSerializer(new Jackson2JsonRedisSerializer<>(Message.class)); // 직렬화 - redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장 + redisTemplateMessage.opsForList().rightPush(roomId, messageDto); // redis 저장 + redisTemplateMessage.expire(roomId, 24, TimeUnit.HOURS); // redis에서 24시간마다 삭제 } } else { // 4. 뒤쪽에 데이터 붙이기 messageList.addAll(redisMessageList);