Skip to content

Commit fe6eb25

Browse files
committed
fix: 랭킹 json 직렬화 문제 해결
- redis 레포지토리에 직렬화 로직 추가 - 테스트및 검증완료
1 parent b0731c1 commit fe6eb25

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/main/java/com/somemore/domains/volunteerrecord/repository/VolunteerRankingRedisRepository.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.somemore.domains.volunteerrecord.repository;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import com.somemore.domains.volunteerrecord.dto.response.VolunteerMonthlyRankingResponseDto;
47
import com.somemore.domains.volunteerrecord.dto.response.VolunteerRankingResponseDto;
58
import com.somemore.domains.volunteerrecord.dto.response.VolunteerTotalRankingResponseDto;
@@ -31,13 +34,29 @@ public void saveRanking(VolunteerRankingResponseDto rankings) {
3134

3235
@SuppressWarnings("unchecked")
3336
public Optional<VolunteerRankingResponseDto> getRankings() {
37+
ObjectMapper mapper = new ObjectMapper();
38+
39+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
40+
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
41+
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
3442

3543
List<VolunteerTotalRankingResponseDto> totalRanking =
36-
(List<VolunteerTotalRankingResponseDto>) redisTemplate.opsForValue().get(TOTAL_RANKING_KEY);
44+
(List<VolunteerTotalRankingResponseDto>) Optional.ofNullable(redisTemplate.opsForValue().get(TOTAL_RANKING_KEY))
45+
.map(obj -> mapper.convertValue(obj,
46+
mapper.getTypeFactory().constructCollectionType(List.class, VolunteerTotalRankingResponseDto.class)))
47+
.orElse(null);
48+
3749
List<VolunteerMonthlyRankingResponseDto> monthlyRanking =
38-
(List<VolunteerMonthlyRankingResponseDto>) redisTemplate.opsForValue().get(MONTHLY_RANKING_KEY);
50+
(List<VolunteerMonthlyRankingResponseDto>) Optional.ofNullable(redisTemplate.opsForValue().get(MONTHLY_RANKING_KEY))
51+
.map(obj -> mapper.convertValue(obj,
52+
mapper.getTypeFactory().constructCollectionType(List.class, VolunteerMonthlyRankingResponseDto.class)))
53+
.orElse(null);
54+
3955
List<VolunteerWeeklyRankingResponseDto> weeklyRanking =
40-
(List<VolunteerWeeklyRankingResponseDto>) redisTemplate.opsForValue().get(WEEKLY_RANKING_KEY);
56+
(List<VolunteerWeeklyRankingResponseDto>) Optional.ofNullable(redisTemplate.opsForValue().get(WEEKLY_RANKING_KEY))
57+
.map(obj -> mapper.convertValue(obj,
58+
mapper.getTypeFactory().constructCollectionType(List.class, VolunteerWeeklyRankingResponseDto.class)))
59+
.orElse(null);
4160

4261
if (totalRanking == null || monthlyRanking == null || weeklyRanking == null) {
4362
return Optional.empty();
@@ -49,4 +68,6 @@ public Optional<VolunteerRankingResponseDto> getRankings() {
4968
weeklyRanking
5069
));
5170
}
71+
72+
5273
}

src/main/java/com/somemore/global/redis/config/RedisConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.somemore.global.redis.config;
22

3+
import com.somemore.domains.volunteerrecord.dto.response.VolunteerRankingResponseDto;
34
import com.somemore.global.common.event.ServerEventType;
45
import lombok.RequiredArgsConstructor;
56
import org.apache.commons.lang3.StringUtils;
@@ -51,6 +52,10 @@ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connec
5152
template.setKeySerializer(new StringRedisSerializer());
5253
template.setValueSerializer(new CustomJacksonRedisSerializer<>(Object.class));
5354

55+
56+
template.setValueSerializer(new CustomJacksonRedisSerializer<>(VolunteerRankingResponseDto.class));
57+
template.setHashValueSerializer(new CustomJacksonRedisSerializer<>(VolunteerRankingResponseDto.class));
58+
5459
return template;
5560
}
5661

0 commit comments

Comments
 (0)