5

I tried following this tutorial: http://javakart.blogspot.in/2012/12/spring-data-redis-hello-world-example.html

My question is related to this: Weird redis key with spring data Jedis

I was able to solve the keys and hashkeys using the StringRedisSerializer .

<bean 
id="stringRedisSerializer" 
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean 
id="redisTemplate" 
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" 
p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer" 
/>

However I found it a problem using a serializer for the value and hashvalue.

I tried adding this:

p:valueSerializer-ref="stringRedisSerializer"
p:hashValueSerializer-ref="stringRedisSerializer"

But an error prompted: "User cannot be cast to java.lang.String"

Can anyone share how to use a serializer for the value/hashvalue?

Community
  • 1
  • 1
Swiper Noswiping
  • 66
  • 1
  • 1
  • 5
  • This seems like you are serialising User object as String Object. instead you can mark User object as Serializable by writing "class User implements java.io.Serializable" then java default serialiser will automatically taken care. So better not configure anything related to "Serializer-ref=" in your beans.xml – Kanagavelu Sugumar Nov 14 '16 at 01:27
  • http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-jedis – Kanagavelu Sugumar Nov 14 '16 at 01:28

1 Answers1

10

Redis stores keys and values as string. It's up to your persistence layer to handle the parsing. In the example, User is a POJO and not a String. I suggest that you use JacksonJsonRedisSerializer instead of StringRedisSerializer. This way you're storing json as your value.

<bean id="userJsonRedisSerializer" 
    class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
    <constructor-arg type="java.lang.Class" value="com.mycompany.redis.domain.User"/>
</bean>
pat
  • 1,737
  • 2
  • 12
  • 16