【异常解决】缓存报错:Null key returned for cache operation (maybe you are using named params on classes withou

Null key returned for cache operation的解决方案

一、背景描述

技术栈:Springboot(2.5.14) + MQTT(5.x) + Cache + MySQL

项目中有一个方法:根据设备id查询设备详情,它是一个非常高频使用的函数。为了减轻对数据库的压力,自然而然的就想到了使用缓存的技术。

在这里插入图片描述

二、报错内容

在这里插入图片描述

Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder

详细报错信息如下图所示:

在这里插入图片描述

三、报错原因

在这里插入图片描述

@Cacheable(key = "#id", unless = "#result==null")

注解中key的值即id值为null,即该方法的入参 id 为空,在调用方法时,不能传null值,传入 null 值的话就会报上述错误。

四、解决方案

在调用该方法的时候先判断id是否为空,id不为空时再调用该方法即可。

比如:
在这里插入图片描述

本文完结!

猜你喜欢

转载自blog.csdn.net/weixin_44299027/article/details/131978708