添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 <?xml version="1.0" encoding="UTF-8"?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.4.1</version > <relativePath /> </parent > <groupId > com.example</groupId > <artifactId > redis-springboot</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > redis-springboot</name > <description > Demo project for Spring Boot</description > <properties > <java.version > 1.8</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-data-redis</artifactId > </dependency > <dependency > <groupId > cn.hutool</groupId > <artifactId > hutool-all</artifactId > <version > 5.4.7</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.11.2</version > <scope > compile</scope > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
redis配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;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.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration public class RedisConfiguration { @Bean public RedisTemplate<String, String> redisTemplate (RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class ) ; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
application.properties
1 2 3 4 5 spring.redis.cluster.nodes =192.168.0.6:6379,192.168.0.7:6379,192.168.0.8:6379,192.168.0.8:6379,192.168.0.10:6379,192.168.0.11:6379
User.java
1 2 3 4 5 6 7 @Data public class User implements Serializable { private String name; private Integer age; }
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.redisspringboot;import cn.hutool.json.JSONUtil;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import javax.annotation.Resource;@SpringBootTest class SimpleTests { @Resource private RedisTemplate<Object,Object> redisTemplate; @Resource private StringRedisTemplate stringRedisTemplate; @Test void setObj () { User user = new User(); user.setAge(11 ); user.setName("anthony" ); redisTemplate.opsForValue().set("setObj" ,user); } @Test void setObjStr () { User user = new User(); user.setAge(11 ); user.setName("anthony" ); redisTemplate.opsForValue().set("setObjStr" , JSONUtil.parse(user).toString()); } @Test void setObjStringredistemplate () { User user = new User(); user.setAge(11 ); user.setName("anthony" ); stringRedisTemplate.opsForValue().set("setObjStringredistemplate" , JSONUtil.parse(user).toString()); } }
运行三个测试方法,会有三种测试结果
第一种:序列化的时候,会带上实体类的包路径,如果再get()
的时候,在微服务可能会报错
第二种:序列化的时候,会序列化两次,因为redis配置文件redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
这里又序列化了一次,所有会看到反斜杠,很不舒服
第三种:才是我想要的结果,只需要序列化一次,而且json可以被redis界面工具识别
StringRedisTemplate
说下StringRedisTemplate
这个类,只能对key=String,value=String的键值对进行操作,RedisTemplate可以对任何类型的key-value键值对操作。
如果在项目中通常都是字符串操作,通常就这个类就已经够用了