Redis 自定义对象 cannot be cast to java.lang.String

Posted zhengwangzw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis 自定义对象 cannot be cast to java.lang.String相关的知识,希望对你有一定的参考价值。

Redis序列化对象的时候报错如下

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)
BHXTZXTask是自己定义的Bean,因此问题就变成了自定义的对象不能转成String对象,为什么会需要转成String对象呢

查看源码,如下,通过rawValue()函数获取序列化之后的字节码

public Long leftPush(K key, V value) {
        final byte[] rawKey = rawKey(key);
        final byte[] rawValue = rawValue(value);
        return execute(new RedisCallback<Long>() {

            public Long doInRedis(RedisConnection connection) {
                return connection.lPush(rawKey, rawValue);
            }
        }, true);
    }
private byte[] rawValue(Object value) {
if (valueSerializer == null && value instanceof byte[]) {
return (byte[]) value;
}
return valueSerializer.serialize(value);
}
 

通过 valueSerializer序列化来序列化对象, redis如果不配置,默认 valueSerializer的序列化是会使用

StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();

        System.out.println(redisTemplate.hasKey("bohai_credit"));
        redisTemplate.delete("bohai_credit");

        System.out.println(stringRedisSerializer.serialize("bohai_credit"));
        System.out.println(jdkSerializationRedisSerializer.serialize("bohai_credit"));

运行结果如上所示。







以上是关于Redis 自定义对象 cannot be cast to java.lang.String的主要内容,如果未能解决你的问题,请参考以下文章

使用redis时出现java.util.ArrayList cannot be cast to java.lang.Long

laravel项目出现Non-static method Redis::hGet() cannot be called statically的解决方法

Spring-Data-Redis--解决java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to xxx

[redis] The type org.apache.commons.pool2.impl.GenericObjectPoolConfig cannot be resolved.It is indi

Android -- android.os.Parcelable[] cannot be cast to ...

The project cannot be built until build path error