java中使用RedisTemplate读取数据异常 Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class' at [Source: (byte[])"
报错:
Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
at [Source: (byte[])"{"name":"测试","phone":"123","idCard":"12345","channelCode":"0"}"; line: 1, column: 66]
原代码:
@Autowired
private RedisTemplate<String, String> redisTemplate;
redisTemplate.opsForValue().set(token, JSONUtil.toJsonStr(currentUserInfo), tokenExpireTime, TimeUnit.SECONDS);
String userInfoRaw = redisTemplate.opsForValue().get(token);
错误原因:
调用redis存储对象时,我用了hutool的JSONUtil.toJsonStr来序列化对象,这个工具类的底层是GenericJackson2JsonRedisSerializer ,它在把对象转化为json时会丢失@class信息,造成redis可以存储,不能读取的bug。
解决方法:
直接储存对象,交给redisTemplate来序列化,或手动使用Jackson2JsonRedisSerializer序列化对象。
修改后代码:
@Autowired
private RedisTemplate<String, Object> redisTemplate;
redisTemplate.opsForValue().set(token, currentUserInfo, tokenExpireTime, TimeUnit.SECONDS);
Object userInfoRaw = redisTemplate.opsForValue().get(token);
标签:java,Object,id,type,class,redisTemplate
From: https://www.cnblogs.com/yiyuzi/p/17351887.html