org.springframework.data.redis.serializer.SerializationException

org.springframework.data.redis.serializer.SerializationException

我有一个从 redis 读取数据的应用程序。我得到以下异常堆栈

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: 7B226C65 at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:41) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.data.redis.core.AbstractOperations.deserializeHashValue(AbstractOperations.java:296) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.data.redis.core.AbstractOperations.deserializeHashMap(AbstractOperations.java:257) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.data.redis.core.DefaultHashOperations.entries(DefaultHashOperations.java:227) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] at com.engine.Template.getDataFromMap(Template.java:38) ~[classes/:?] at com.engine.Utils.getLevelInfoFrom(Utils.java:199) ~[classes/:?] at com.engine.Utils.getData(Utils.java:368) ~[classes/:?] at com.engine.controller.TicketController.process(TicketController.java:56) ~[classes/:?] at com.engine.jms.JmsMessageReceiver.onMessage(JmsMessageReceiver.java:56) [classes/:?] at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:746) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:684) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:651) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:315) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:253) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1150) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1142) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1039) [spring-jms-4.2.4.RELEASE.jar:4.2.4.RELEASE] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_74] 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: 7B226C65 at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:78) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:39) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] ... 17 more Caused by: java.io.StreamCorruptedException: invalid stream header: 7B226C65 at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806) ~[?:1.8.0_74] at java.io.ObjectInputStream.(ObjectInputStream.java:299) ~[?:1.8.0_74] at org.springframework.core.ConfigurableObjectInputStream.(ConfigurableObjectInputStream.java:64) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.ConfigurableObjectInputStream.(ConfigurableObjectInputStream.java:50) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:68) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:73) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:36) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:39) ~[spring-data-redis-1.4.2.RELEASE.jar:1.4.2.RELEASE] ... 17 more

我也试过在其他论坛上搜索解决方案,但没有看到任何解决方案。下面是我的代码块

String receivedJSON = (String) redisTemplate.opsForHash().get(redisKey, hashKey);

如果有人找到任何解决方案甚至遇到这个问题,请回复。

我找到了解决方案。问题是 hashValue 应该使用字符串序列化程序进行序列化。将默认序列化程序设置为字符串序列化程序后,我找到了解决方案并完成了我的要求。

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="redisConnectionFactory">
        <property name="defaultSerializer" ref="stringSerializer" />
    </bean>

    <bean id="stringSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />