先上问题:
java.lang.ClassCastException: com.ppdai.cbd.ddp.thirdparty.contract.bhxtzx.BHXTZXTask cannot be cast to java.lang.String at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:33) at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:117) at org.springframework.data.redis.core.DefaultListOperations.leftPush(DefaultListOperations.java:71) at org.springframework.data.redis.core.DefaultBoundListOperations.leftPush(DefaultBoundListOperations.java:60) at com.ppdai.realtime.datachannel.pullservice.entity.RedisQueue.pushFromHead(RedisQueue.java:63) at com.ppdai.realtime.datachannel.pullservice.redisconfig.RedisTaskSender.sendTask(RedisTaskSender.java:35) at com.ppdai.realtime.datachannel.pullservice.redisconfig.RedisTaskSender$sendTask.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) at com.ppdai.realtime.datachannel.pullservice.dataproviders.bohai.BohaiQueryProvider.afterGetData(BohaiQueryProvider.groovy:395) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
看问题原因是序列化对象的时候使用的是 StringRedisSerializer, 本来以为是因为AutoConfig 导致加载了系统默认的RedisTemplate, 而自己定义了RedisTemplate没有加载,
但是非常疑惑的一点是默认的RedisTemplate使用的是 JdkSerializationRedisSerializer(如下代码),上面异常报的是使用redis value 使用StringRedisSerializer 导致的类型转化失败
public void afterPropertiesSet() { super.afterPropertiesSet(); boolean defaultUsed = false; if (defaultSerializer == null) { defaultSerializer = new JdkSerializationRedisSerializer( classLoader != null ? classLoader : this.getClass().getClassLoader()); } if (enableDefaultSerializer) { if (keySerializer == null) { keySerializer = defaultSerializer; defaultUsed = true; } if (valueSerializer == null) { valueSerializer = defaultSerializer; defaultUsed = true; } if (hashKeySerializer == null) { hashKeySerializer = defaultSerializer; defaultUsed = true; } if (hashValueSerializer == null) { hashValueSerializer = defaultSerializer; defaultUsed = true; } } if (enableDefaultSerializer && defaultUsed) { Assert.notNull(defaultSerializer, "default serializer null and not all serializers initialized"); } if (scriptExecutor == null) { this.scriptExecutor = new DefaultScriptExecutor<K>(this); } initialized = true; }
最后通过Alt + F7 (Find Usage) 发现下面这个代码
private Long incr(String key, long delta){ RedisTemplate redisTemplate = getRedisTemplate() ValueOperations<String, String> operations = redisTemplate.opsForValue() redisTemplate.setKeySerializer(new StringRedisSerializer()) redisTemplate.setValueSerializer(new StringRedisSerializer()) return operations.increment(key, delta) }
原来就是这里搞得鬼,将redisTemplate的ValueSerializer设置成了 StringRedisSerializer ,StringRedisSerializer参数是String类型的,因此就出现了类型转化错误。
附说明
redisTemplate需要序列化key,value,hashkey,hashvalue, 提供了很多序列化工具,什么Jackson、FastJson、JDKSerialization 也可以自己定义。
redisTamplate默认这四种都使用JDKSerialization做序列化,如果没有提供定义默认都用JDK。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:[Redis] ** cannot be cast to java.lang.String - Python技术站