将没有实现Serializable的类存储到Redis中方法示例
目录
- 将实例对象存储到Redis中报错
- ObjectHashMapper
- Jackson2HashMapper
- BeanUtilsHashMapper
将实例对象存储到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 { /** php * 用户名 */ private String username; /** * 年龄 */ private Integer age; /** * 所属部门 */ private Department department; /** * 创建时间 */ private Date createTime; /** * 手机号码 */ private List<String> phoneNumber; /** * 爱好 */ private Map<String, Object> hobby; }
部门类
@Data @Builder @NoArgsConst编程客栈ructor @AllArgsConstructor public class Department { /** * 部门id */ private androidString depId; /** * 部门名称 */ private String depName; /** * 父级部门 */ private String parentId; }
注意:上面两个类都没有实现序列化
通过ObjectHashMapper的toHash和fromHash实现
List<String> phoneNumbers =www.devze.com new ArrayList<>(); phoneNumbers.add("13016541724"); phoneNumbers.add("18826173452"); Map<String, Object> 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<byte[], byte[]> map = objectHashMapper.toHash(userEntity); redisTemplate.opsForHash().put("TEST_OBJECT", "a", map); Map<byte[], byte[]> map1 = (Map<byte[], byte[]>) redisTemplate.opsForHash().get("TEST_OBJECT", "a"); UserEntity userEntityRes = (UserEntity) objectHashMapper.fromHash(map1);
Jackson2HashMapper
Jackson2HashMapper
通过使用FasterXMLJackson
为域对象提供Redis
哈希映射。可以将顶级属性映射为哈希字段名,也可以选择展平结构。简单类型映射到简单值。复杂类型(嵌套对象、集合、映射等)表示为嵌套jsON。Jackson2HashMapper
平展为所有嵌套属性创建单独的哈希条目,并尽可能将复杂类型解析为简单类型。
List<String> phoneNumbers = new ArrayList<>(); phoneNumbers.add("13016541724"); phoneNumbers.add("18826173452"); Map<String, Object> 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<String, Object> map = jackson2HashMapper.toHash(userEntity); redisTemplate.opsForHash().put("TEST_O开发者_JS培训BJECT", "a", map); Map<String, Object> map1 = (Map<String, Object>) redisTemplate.opsForHash().get("TEST_OBJECT", "a"); UserEntity userEntityRes = (UserEntity) jackson2HashMapper.fromHash(map1);
BeanUtilsHashMapper
BeanUtilsHashMapper
使用Spring的BeanUtils。
List<String> phoneNumbers = new ArrayListjs<>(); phoneNumbers.add("13016541724"); phoneNumbers.add("18826173452"); Map<String, Object> 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<String, Object> map = beanUtilsHashMapper.toHash(userEntity); redisTemplate.opsForHash().put("TEST_OBJECT", "a", map); Map<String, Object> map1 = (Map<String, Object>) 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
从报错中可以看出,List
为interface
,无法转换。将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后变成了一个手机号码
这是BeanUtilsHashMapper
的一个缺陷。
以上就是将没有实现Serializable的类存储到Redis中方法示例的详细内容,更多关于Redis存储Serializable类的资料请关注我们其它相关文章!
精彩评论