unable to serialize problems when Redis storage Hash-map __string

Source: Internet
Author: User
Tags serialization

Reprint please indicate the source: http://blog.csdn.net/qq_26525215

This article originates from "University trip------familiar with the blog of Memory"

There was a problem today when using Redis's map to store beans.
The questions are as follows:

Java.lang.ClassCastException:com.uifuture.TestBean cannot is cast to java.lang.String at org.springframework.data.red Is.serializer.StringRedisSerializer.serialize (stringredisserializer.java:32) at Org.springframework.data.redis.core.AbstractOperations.rawHashValue (abstractoperations.java:168) at Org.springframework.data.redis.core.DefaultHashOperations.put (defaulthashoperations.java:168) at Org.springframework.data.redis.core.DefaultBoundHashOperations.put (defaultboundhashoperations.java:90) at Com.uifuture.utils.RedisUtil.addMap (redisutil.java:299) at Com.uifuture.utils.redis.SaveMapCallable.call ( savemapcallable.java:33) at Java.util.concurrent.futuretask$sync.innerrun (futuretask.java:334) at Java.util.concur Rent. Futuretask.run (futuretask.java:166) at Java.util.concurrent.ThreadPoolExecutor.runWorker (Threadpoolexecutor.java : 1110) at Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:603) at Java.lang.Thread.run ( Thread.java:722) 

My project uses Spring-data-redis management Redis and serialization.

So when I was configuring it in the spring configuration file, I configured the
Keyserializer: For ordinary k-v operations, key takes a serialization strategy
Serialization strategy taken by Valueserializer:value
These 2 serialization strategies.

In fact, I was less configured with another 2 serialization policies.
The consequence is that when storing ordinary beans, there is no problem, which leads me more than 10 days from the start of the project to the present.

In fact, the solution is very simple, I have less configuration
Hashkeyserializer: In the hash data structure, the Hash-key serialization strategy
Serialization strategy for Hashvalueserializer:hash-value
The two serialization policies.

At the time, my spring's configuration serialization strategy was:

<!--If you do not configure serializer, you can only use String when storing, and if you store it with an object type, you will be prompted with the error can ' t cast to String ... -->
        <property name= "Keyserializer" >
            <!--The default serializer for key. The default value is stringserializer-->
            <bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <!--is the default serializer for value, and the default value is Jdkserializationredisserializer from Defaultserializer. -->
        <property name= "ValueSerializer" >
            <bean class= " Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer "/>
        </property>

Need to add two more, that is, change to:

<!--If you do not configure serializer, you can only use String when storing, and if you store it with an object type, you will be prompted with the error can ' t cast to String ... --> <property name= "Keyserializer" > <!--the default serializer for key. The default value is stringserializer--> <bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer"/&G
        T </property> <!--is the default serializer for value, and the default value is Jdkserializationredisserializer from Defaultserializer. --> <property name= "ValueSerializer" > <bean class= "Org.springframework.data.redis.serializ Er. Jdkserializationredisserializer "/> </property> <!--the serialization configuration required by the key when storing the map--> <propert Y name= "Hashkeyserializer" > <bean class= "Org.springframework.data.redis.serializer.StringRedisSerializer "/> </property> <!--serialization configuration of value needed to store a map--> <property name=" Hashvalueserializer "&
            Gt <bean class= "Org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> 

This solves the problem of unable to serialize the map.

That is, when you remember to write a serialization strategy, remember to write all four of them at once.
I check this mistake, just read a lot of blog to find that I write less.
I almost had to get myself a tool class for serialization and deserialization.

Some of the other Redis basic knowledge, tools, ah, do not write out. After all, it's just a blog that solves problems,

This article is written by [familiar memory], all rights reserved.
Welcome to reprint, sharing is the source of progress.

Reprint please indicate the source: http://blog.csdn.net/qq_26525215

This article originates from "University trip------familiar with the blog of Memory"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.