当前位置: 代码迷 >> 综合 >> 关闭spring boot jackson的FAIL_ON_EMPTY_BEANS
  详细解决方案

关闭spring boot jackson的FAIL_ON_EMPTY_BEANS

热度:25   发布时间:2024-01-16 06:47:39.0

发生异常错误信息(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(RedisConnectionFactoryredisConnectionFactory) {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;
}


  相关解决方案