问题描述
在开发中遇到一个问题,以前没有注意到过。
就是springboot默认的redisTemplate在存储数据到redis时,由于他的key和value的serializer都是
JdkSerializationRedisSerializer
所以,在序列化进redis后的key和value都是乱码,如:
这还不是重点,重点是,再从redis中读取的时候,读取回来的数据就缺少内容了!比如我的实体类是Session2:
package com.test.test;
import com.test.test.entity.User2;
import java.io.Serializable;
public class SessionUser2 implements Serializable{
private static final long serialVersionUID = 8623691300532528400L;
private String token;
private User2 user;
public SessionUser2() {
}
public SessionUser2(String token, User2 user) {
this.token = token;
this.user = user;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public User2 getUser() {
return user;
}
public void setUser(User2 user) {
this.user = user;
}
}
其中的User2是一个实体类:
package com.jfqqq.test.entity;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
public class User2 extends BasePojo implements Serializable {
private static final long serialVersionUID = 4384720853268686063L;
@NotBlank(message = "{user.uuid.notblank}")
private String uuid;
@NotBlank(message = "{user.email.notblank}")
private String loginEmail;
@NotBlank(message = "{user.phone.notblank}")
private String phone;
private Long itemId;
public User2() {
}
public User2(String uuid, String loginEmail, String phone, Long itemId) {
this.uuid = uuid;
this.loginEmail = loginEmail;
this.phone = phone;
this.itemId = itemId;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getLoginEmail() {
return loginEmail;
}
public void setLoginEmail(String loginEmail) {
this.loginEmail = loginEmail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Long getItemId() {
return itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
}
package com.jfqqq.test.entity;
import java.util.Date;
/**
* 基礎实体类
*
* @author ack
*
*/
public class BasePojo {
protected Long id;
protected Long creator;
protected String createName;
protected Long modifier;
protected Date createTime;
protected Date modifyTime;
protected String modifyName;
protected Integer deleteStatus = -1;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getCreator() {
return creator;
}
public void setCreator(Long creator) {
this.creator = creator;
}
public String getCreateName() {
return createName;
}
public void setCreateName(String createName) {
this.createName = createName;
}
public Long getModifier() {
return modifier;
}
public void setModifier(Long modifier) {
this.modifier = modifier;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyDate) {
this.modifyTime = modifyDate;
}
public Integer getDeleteStatus() {
return deleteStatus;
}
public void setDeleteStatus(Integer deleteStatus) {
this.deleteStatus = deleteStatus;
}
public String getModifyName() {
return modifyName;
}
public void setModifyName(String modifyName) {
this.modifyName = modifyName;
}
}
要序列化的数据:
反序列化再读取回来后,id没有值了:
原因分析
basePojo没有implements Serializable接口,因为默认的redisTemplate的key和value都是JdkSerializationRedisSerializer,没有实现接口的化就会无法序列化,甚至报错,报错见:
redis序列化实体类对象报错_jfqqqqq的博客-CSDN博客
解决方案
方案一
basePojo也实现Serializable接口
方案二
或者干脆自定义序列化器,不需要都实现Serializable接口那么麻烦,而且顺便还解决了redis中查看数据时看天书看不懂的问题:
咱们自己定义一个redisTemplate,设定序列化器,这样springboot启动时就不会创建它自己要创建的那个template了。
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
return redisTemplate;
}
}