redisTemplate 保存AtomicInteger对象异常:
java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String
at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)
at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)
at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139)
跟踪源码发现其执行的是 StringRedisSerializer 的实现,serialize默认接收的参数类型为String 从而抛出以上异常
经过检查,发现是RedisTemplate泛型惹的祸:
@Autowired
private RedisTemplate<String, String> redisTemplate;
解决方案,去除泛型:
@Autowired
private RedisTemplate redisTemplate;
运行服务再次跟踪源码,执行的是 DefaultValueOperations 的实现,问题解决