[Redis] ** cannot be cast to java.lang.String


先上問題:

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。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: