[Spring + Redis] 오류 org.springframework.data.redis.serializer.SerializationException: Cannot deserialize;

2022. 7. 29. 16:52JAVA

반응형

< 레디스 초보 주의... >

 

 

레디스의 scan 명령을 사용해서 키 목록을 조회하는 메서드를 만들고 있었습니다.

그런데, 계속 org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; 오류가 발생합니다.

코드는 이렇습니다.

 

@Scheduled(fixedRate = scheduledIncreaseSeconds, timeUnit = TimeUnit.SECONDS)
public void scheduledIncreasePostHitCounts() {
    log.debug("스케줄 조회수 업데이트 태스크 {}", LocalDateTime.now());
    // 리스트에 존재하는 키들 빼와서 update쿼리 날리기
    ScanOptions scanOption = ScanOptions.scanOptions().match(scheduleHitCountKeyPrefix + "*").build();
    List<String> keys = redisTemplate.scan(build).stream().map(i -> i.getBytes(StandardCharsets.UTF_8).toString()).collect(Collectors.toList());
    //postRepository.updatePostsHitCounts();
}

6번라인에서 오류가 발생합니다.

 

왜 그런가 원인을 찾아보니.. redisTemplate을 잘못 사용하고 있었습니다.

레디스는 아직 한글 자료가 별로 없더군요,,,

토익 830점의 힘을 체감하며 

 

https://stackoverflow.com/questions/47882330/org-springframework-data-redis-serializer-serializationexception

 

org.springframework.data.redis.serializer.SerializationException

I have an application which reads data from redis. I am getting the below exception stack org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception ...

stackoverflow.com

 

레디스템플릿을 범용으로 쓰려고 했었습니다.

private final RedisTemplate<String, Object> redisTemplate;

key는 String으로, value는 Object로 선언해두고 사용하고 있었는데, 여기서 뭔가 잘 안되었던 모양입니다

 

private final RedisTemplate<String, String> stringRedisTemplate;

이렇게 레디스 템플릿을 또 만들어서 사용해도 되는건지 잘 모르겠지만, 만들어놓으니 작동은 잘 하는군요

 

 

해결~

 

 

org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.StreamCorruptedException: invalid stream header: 73636865
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:84) ~[spring-data-redis-2.7.0.jar:2.7.0]
at org.springframework.data.redis.core.RedisTemplate.deserializeKey(RedisTemplate.java:597) ~[spring-data-redis-2.7.0.jar:2.7.0]
at org.springframework.data.redis.core.ConvertingCursor.next(ConvertingCursor.java:65) ~[spring-data-redis-2.7.0.jar:2.7.0]
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) ~[na:na]
at org.springframework.data.util.IteratorSpliterator.forEachRemaining(IteratorSpliterator.java:88) ~[spring-data-commons-2.7.0.jar:2.7.0]
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
at com.zzangmin.gesipan.web.service.RedisService.scheduledIncreasePostHitCounts(RedisService.java:42) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.20.jar:5.3.20]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.20.jar:5.3.20]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]
Caused by: org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.StreamCorruptedException: invalid stream header: 73636865
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78) ~[spring-core-5.3.20.jar:5.3.20]
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36) ~[spring-core-5.3.20.jar:5.3.20]
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:82) ~[spring-data-redis-2.7.0.jar:2.7.0]
... 22 common frames omitted
Caused by: java.io.StreamCorruptedException: invalid stream header: 73636865
at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:940) ~[na:na]
at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:379) ~[na:na]
at org.springframework.core.ConfigurableObjectInputStream.<init>(ConfigurableObjectInputStream.java:65) ~[spring-core-5.3.20.jar:5.3.20]
at org.springframework.core.ConfigurableObjectInputStream.<init>(ConfigurableObjectInputStream.java:51) ~[spring-core-5.3.20.jar:5.3.20]
at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:70) ~[spring-core-5.3.20.jar:5.3.20]
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:73) ~[spring-core-5.3.20.jar:5.3.20]
... 24 common frames omitted

반응형