Skip to content

Commit eeef7ab

Browse files
authored
fix/327 랭킹 json 직렬화 문제 해결 (#336)
* fix: 랭킹 json 직렬화 문제 해결 - redis 레포지토리에 직렬화 로직 추가 - 테스트및 검증완료 * test: 누락된 테스트 클래스 추가
1 parent b0731c1 commit eeef7ab

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
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

src/test/java/com/somemore/domains/volunteerrecord/service/GetVolunteerRankingServiceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ void getVolunteerRanking_Success() {
7878
.hasSize(2)
7979
.extracting("volunteerId", "totalHours", "ranking", "nickname")
8080
.containsExactly(
81-
tuple(id1.toString(), 100, 1, "봉사자1"),
82-
tuple(id2.toString(), 90, 2, "봉사자2")
81+
tuple(id1, 100, 1L, "봉사자1"),
82+
tuple(id2, 90, 2L, "봉사자2")
8383
);
8484

8585
assertThat(result.volunteerMonthlyRankingResponse())
8686
.hasSize(2)
8787
.extracting("volunteerId", "totalHours", "ranking", "nickname")
8888
.containsExactly(
89-
tuple(id3.toString(), 50, 1, "봉사자3"),
90-
tuple(id4.toString(), 40, 2, "봉사자4")
89+
tuple(id3, 50, 1L, "봉사자3"),
90+
tuple(id4, 40, 2L, "봉사자4")
9191
);
9292

9393
assertThat(result.volunteerWeeklyRankingResponse())
9494
.hasSize(2)
9595
.extracting("volunteerId", "totalHours", "ranking", "nickname")
9696
.containsExactly(
97-
tuple(id5.toString(), 30, 1, "봉사자5"),
98-
tuple(id6.toString(), 20, 2, "봉사자6")
97+
tuple(id5, 30, 1L, "봉사자5"),
98+
tuple(id6, 20, 2L, "봉사자6")
9999
);
100100

101101
}

src/test/java/com/somemore/domains/volunteerrecord/service/RankingCacheServiceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,24 @@ void cacheRanking_Success() {
7373
.hasSize(2)
7474
.extracting("volunteerId", "totalHours", "ranking")
7575
.containsExactly(
76-
tuple(id1.toString(), 100, 1),
77-
tuple(id2.toString(), 90, 2)
76+
tuple(id1, 100, 1L),
77+
tuple(id2, 90, 2L)
7878
);
7979

8080
assertThat(dto.volunteerMonthlyRankingResponse())
8181
.hasSize(2)
8282
.extracting("volunteerId", "totalHours", "ranking")
8383
.containsExactly(
84-
tuple(id3.toString(), 50, 1),
85-
tuple(id4.toString(), 40, 2)
84+
tuple(id3, 50, 1L),
85+
tuple(id4, 40, 2L)
8686
);
8787

8888
assertThat(dto.volunteerWeeklyRankingResponse())
8989
.hasSize(2)
9090
.extracting("volunteerId", "totalHours", "ranking")
9191
.containsExactly(
92-
tuple(id5.toString(), 30, 1),
93-
tuple(id6.toString(), 20, 2)
92+
tuple(id5, 30, 1L),
93+
tuple(id6, 20, 2L)
9494
);
9595
});
9696
}

0 commit comments

Comments
 (0)