发生异常错误信息(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):
@Bean public 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 */ @Bean public RedisTemplate<Long, UserStudentInfo> userStudentRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Long, UserStudentInfo> redisTemplate = new RedisTemplate<>(); Jackson2JsonRedisSerializer<UserStudentInfo> 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; }