Reputation: 4404
I'm studing Spring Data for Redis, but until now I have not found any example about how to use the serializers supported by this project?
I've read the section 4.6 of the reference documentation of the project ( http://static.springsource.org/spring-data/data-redis/docs/current/reference/html/redis.html#redis:serializer ) but it basically only says that it exists. Nothing more. How can I use this feature?
Upvotes: 3
Views: 4848
Reputation: 12431
In your spring config
<bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<property name="keySerializer" ref="stringSerializer"/>
<property name="valueSerializer" ref="stringSerializer"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="myhostname" p:port="6379"/>
Or if you want to set it in Java
// inject the actual template
@Autowired
private RedisTemplate<String, Object> template;
...
...
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
Upvotes: 0
Reputation: 632
Serializers are used in a few places in the codebase, most notably in RedisTemplate to convert the raw bytes stored in Redis as keys/values to your custom data types (and vice versa). This is mentioned in Section 4.4 of the documentation.
Upvotes: 2