本文共 1771 字,大约阅读时间需要 5 分钟。
发生异常错误信息(redis缓存处理序列化时):
org.springframework.data.redis.serializer.SerializationException: Could not write JSON: No serializer found for class org.springframework.cache.interceptor.SimpleKey and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.springframework.cache.interceptor.SimpleKey and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
解决办法(定义一个ObjectMapper Bean,jackson2JsonRedisSerializer设置ObjectMapper为自己定义的Bean):
@Beanpublic ObjectMapper objectMapper() { return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);}/** * Integer redis template redis template. * * @param redisConnectionFactory the redis connection factory * @return the redis template */@Beanpublic RedisTemplateuserStudentRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<> (UserStudentInfo.class); jackson2JsonRedisSerializer.setObjectMapper(objectMapper()); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(jackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate;}
转载地址:http://ieadi.baihongyu.com/