文章目录
- 问题
- 原序列化配置
- 修改配置
- 解决方法
问题
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of org.springframework.security.core.authority.SimpleGrantedAuthority (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (byte[])"…
原序列化配置
@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(factory);GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// 使用 StringRedisSerializer 来序列化和反序列化key 的值redisTemplate.setKeySerializer(stringRedisSerializer);redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);// hash的key也采用StringRedisSerializer 的序列化方式redisTemplate.setHashKeySerializer(stringRedisSerializer);redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}
修改配置
@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(factory);// String的序列化StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// json序列化配置GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();// key采用String的序列化方式redisTemplate.setKeySerializer(stringRedisSerializer);// value的序列化方式采用fastJson的方式redisTemplate.setValueSerializer(fastJsonRedisSerializer);// hash的key也采用String 的序列化方式redisTemplate.setHashKeySerializer(stringRedisSerializer);// hash的value序列化方式采用fastJsonredisTemplate.setHashValueSerializer(fastJsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}
解决方法
使用 GenericFastJsonRedisSerializer
替换 GenericFastJsonRedisSerializer