关闭spring boot jackson的FAIL_ON_EMPTY_BEANS

10 篇文章 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(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;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值