SpringBoot使用缓存报错java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee


SpringBoot使用缓存报错java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee

报错如下:

java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee com.example.controller.EmployeeController.getEmpById(java.lang.Integer)] caches=[] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@540d3d5d'. At least one cache should be provided per cache operation.

解决方法:

1、在SpringBoot启动类上添加@EnableCaching注解。

2、在controller中需要加入缓存的方法中添加@Cacheable,需要指定cacheNames缓存组件的名称。

@Cacheable(cacheNames = "emp")
@GetMapping("/getEmpById/{id}")
public Employee getEmpById(@PathVariable("id") Integer id) {
    return employeeService.getEmpById(id);
}

例如上面需要指定cacheNames的值,名字随意。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM