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