引入spring-boot-starter-data-redis组件存在序列化问题的解决
一般在项目中会使用到redis,所以在pom.xml中我们会引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
但是如果使用
@RequestMapping("/set")
public Object set(HttpServletRequest request) {
int id = Integer.parseInt(request.getParameter("id"));
int offset = Integer.parseInt(request.getParameter("offset"));
System.out.println("接收的参数:id:"+id+" offset:"+offset);
redisTemplate.opsForValue().setBit("redisBloomFilter:"+id, offset, true);
return null;
}
通过可视化工具查看

发现前面会存在着乱码
这样不利于我们使用redis-cli进行操作,因为有着乱码所以查找起来就很难找到
为了解决这个问题就必然是要通过设置序列化方式
package com.wb.config; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig { /** * 定制redisTemplet 作用 存储到redis中的对象以序列化的方式 * @param redisConnectionFactory * @return */ @Bean public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //定义value序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer());//定义key序列化方式 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }

浙公网安备 33010602011771号