spring-data-redis redisTemplate Exception
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring-data-redis redisTemplate Exception,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2655字,纯文字阅读大概需要4分钟。
内容图文
![spring-data-redis redisTemplate Exception](/upload/InfoBanner/zyjiaocheng/901/3dc32540638d4b229b618790c57b5c15.jpg)
当我调用get()方法时,发生了异常
这是代码
@Service("RedisService")
public class RedisServiceImpl implements RedisService {
@Autowired
RedisTemplate<String, Long> redisTemplate;
@Override
public Long get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public Long incrBy(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
当我使用incrBy方法时,没有例外但只有错误只有get方法
这是堆栈跟踪—
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:198)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:42)
at net.daum.air21.bot.common.service.RedisServiceImpl.get(RedisServiceImpl.java:29)
at net.daum.air21.bot.user.service.SeraCoffeeServiceImpl.getCurrentCount(SeraCoffeeServiceImpl.java:41)
解决方法:
默认情况下,RedisTemplate使用JdkSerializationRedisSerializer,因此如果你执行了“set”,它会使你的Long在Redis中看起来像这样:
"\xac\xed\x00\x05sr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\x00\x04"
IncrBy有效,因为Redis总是从该操作返回Long,因此RedisTemplate不会尝试反序列化结果.然而,“get”的结果经历了反序列化过程,该过程期望具有如上所述的格式.
您可以通过在RedisTemplate上使用不同的值序列化器来解决此问题:
redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
或者尝试spring-data-redis附带的RedisAtomicLong类.
内容总结
以上是互联网集市为您收集整理的spring-data-redis redisTemplate Exception全部内容,希望文章能够帮你解决spring-data-redis redisTemplate Exception所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。