由于控制台的原因,在springboot项目中不进行序列化直接进行存储或导致redis数据库中出现与预期不符,所以需要在我们自定义的Redistemplate中配置自定义的序列化而不是用源码中的序列化
需要引入的包有spring-boot-starter-data-redis需要在生成springboot项目时勾选 spring Data Redis(Access + Driver)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
resources中application.yml的redis配置:
spring:
redis:
host: 0.0.0.0
port: 6379
password: 123456
RedisConfig.java中尽行了自定义的序列化配置将源码中的RedisTemplate<Object, Object>改为了我们自定义的RedisTemplate<String, Object>方便我们使用因为一般使用的泛型为<String, Object>。
代码如下:
package com.kuang.config;
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;
import java.net.UnknownHostException;
@Configuration
public class RedisConfig {
/**
* 固定模板,拿去直接使用
* @param redisConnectionFactory
* @return template
* @throws UnknownHostException
*/
// 编写自己的redisTemplate
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 配置具体的序列化方式
//json的序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key 采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash 的key 也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value的序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
下面测试用的pojo:
package com.kuang.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User implements Serializable {
private String name;
private Integer age;
}
springboot中的test:
package com.kuang;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.kuang.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
@SpringBootTest
class Redis02SpringbootApplicationTests {
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;
@Test
public void test() throws JsonProcessingException {
// 真实开发一般都是用json来传递对象
User user = new User("hyq", 99);
redisTemplate.opsForValue().set("user", user);
System.out.println(redisTemplate.opsForValue().get("user"));
}
}
结果正常返回 User(name=hyq, age=99)
redis中数据正常显示:
[
"com.kuang.pojo.User",
{
"name": "hyq",
"age": 99
}
]
鄙人知识有限,自学习狂神说redis后编写有不对的地方请大家见谅!