将没有实现Serializable的类存储到Redis中方法示例

将实例对象存储到Redis中报错

你有没有遇到过这种情况,需要将实例对象存储到Redis中,但是类的字段包含第三方依赖中的类并且此类没有 implements Serializable。 执行时就会报没有序列化的错:

Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.entity.UserEntity]
	at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43)
	at org.springframework.core.serializer.Serializer.serializeToByteArray(Serializer.java:56)
	at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:60)
	... 54 more

有三种方法如下

ObjectHashMapper

ObjectHashMapper使用对象到哈希映射。

需存redis的类如下:

用户类

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
    /**
     * 用户名
     */
    private String username;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 所属部门
     */
    private Department department;
    /**
     * 创建时间
     */
    private Date createTime;
    /**
     * 手机号码
     */
    private List phoneNumber;
    /**
     * 爱好
     */
    private Map hobby;
}

部门类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Department {
    /**
     * 部门id
     */
    private String depId;
    /**
     * 部门名称
     */
    private String depName;
    /**
     * 父级部门
     */
    private String parentId;
}

注意:上面两个类都没有实现序列化

通过ObjectHashMapper的toHash和fromHash实现

List phoneNumbers = new ArrayList<>();
phoneNumbers.add("13016541724");
phoneNumbers.add("18826173452");
Map hobbys = new HashMap<>();
hobbys.put("运行", "打羽毛球");
hobbys.put("电影", "看电影");
UserEntity userEntity = UserEntity.builder().username("张三").age(18).department(
        Department.builder().depId("djinefn213445nkqk").depName("研发中心").parentId("ncdjkn3j4njndcinw").build()
).createTime(new Date()).phoneNumber(phoneNumbers).hobby(hobbys)
        .build();
ObjectHashMapper objectHashMapper = new ObjectHashMapper();
Map map = objectHashMapper.toHash(userEntity);
redisTemplate.opsForHash().put("TEST_OBJECT", "a",  map);
Map map1 = (Map) redisTemplate.opsForHash().get("TEST_OBJECT", "a");
        UserEntity userEntityRes = (UserEntity) objectHashMapper.fromHash(map1);

Jackson2HashMapper

Jackson2HashMapper通过使用FasterXMLJackson为域对象提供Redis哈希映射。可以将顶级属性映射为哈希字段名,也可以选择展平结构。简单类型映射到简单值。复杂类型(嵌套对象、集合、映射等)表示为嵌套JSON。Jackson2HashMapper平展为所有嵌套属性创建单独的哈希条目,并尽可能将复杂类型解析为简单类型。

List phoneNumbers = new ArrayList<>();
phoneNumbers.add("13016541724");
phoneNumbers.add("18826173452");
Map hobbys = new HashMap<>();
hobbys.put("运行", "打羽毛球");
hobbys.put("电影", "看电影");
UserEntity userEntity = UserEntity.builder().username("张三").age(18).department(
        Department.builder().depId("djinefn213445nkqk").depName("研发中心").parentId("ncdjkn3j4njndcinw").build()
).createTime(new Date()).phoneNumber(phoneNumbers).hobby(hobbys)
        .build();
Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(true);
Map map = jackson2HashMapper.toHash(userEntity);
redisTemplate.opsForHash().put("TEST_OBJECT", "a",  map);
Map map1 = (Map) redisTemplate.opsForHash().get("TEST_OBJECT", "a");
UserEntity userEntityRes = (UserEntity) jackson2HashMapper.fromHash(map1);

BeanUtilsHashMapper

BeanUtilsHashMapper使用Spring的BeanUtils。

List phoneNumbers = new ArrayList<>();
phoneNumbers.add("13016541724");
phoneNumbers.add("18826173452");
Map hobbys = new HashMap<>();
hobbys.put("运行", "打羽毛球");
hobbys.put("电影", "看电影");
UserEntity userEntity = UserEntity.builder().username("张三").age(18).department(
        Department.builder().depId("djinefn213445nkqk").depName("研发中心").parentId("ncdjkn3j4njndcinw").build()
).createTime(new Date()).phoneNumber(phoneNumbers).hobby(hobbys)
        .build();
BeanUtilsHashMapper beanUtilsHashMapper = new BeanUtilsHashMapper(UserEntity.class);
Map map = beanUtilsHashMapper.toHash(userEntity);
redisTemplate.opsForHash().put("TEST_OBJECT", "a",  map);
Map map1 = (Map) redisTemplate.opsForHash().get("TEST_OBJECT", "a");
UserEntity userEntityRes = (UserEntity) beanUtilsHashMapper.fromHash(map1);

运行会报

Caused by: org.apache.commons.beanutils.ConversionException: Can't convert value '' to type interface java.util.List
	at org.apache.commons.beanutils.converters.AbstractConverter.conversionException(AbstractConverter.java:474)
	at org.apache.commons.beanutils.converters.StringConverter.convertToType(StringConverter.java:96)
	at org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:312)
	... 58 more

从报错中可以看出,Listinterface,无法转换。将List改为ArrayList,Map改为HashMap再执行报如下错误

Caused by: org.apache.commons.beanutils.ConversionException: Can't convert value '' to type class java.util.ArrayList
	at org.apache.commons.beanutils.converters.AbstractConverter.conversionException(AbstractConverter.java:474)
	at org.apache.commons.beanutils.converters.StringConverter.convertToType(StringConverter.java:96)
	at org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:312)
	... 58 more

排查会发现,phoneNumber的两个手机号码toHash后变成了一个手机号码

将没有实现Serializable的类存储到Redis中方法示例_第1张图片

这是BeanUtilsHashMapper的一个缺陷。

以上就是将没有实现Serializable的类存储到Redis中方法示例的详细内容,更多关于Redis存储Serializable类的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(将没有实现Serializable的类存储到Redis中方法示例)