解决Spring boot3.x @Cacheable 使用参数名报错 java.lang.IllegalArgumentException: Null key returned for cac...

  • 使用版本::: Spring Boot :: v3.4.5
    使用本地缓存时报以下的错误,怎么解决呢?
2025-05-30 00:11:52 ERROR [http-nio-8080-exec-3] o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: Null key returned for cache operation [Builder[public com.wewetea.open.weadmin.model.po.User com.wewetea.open.weadmin.service.impl.UserServiceImpl.queryUserById(java.lang.Long)] caches=[caffeineCacheManager] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='']. If you are using named parameters, ensure that the compiler uses the '-parameters' flag.] with root cause
java.lang.IllegalArgumentException: Null key returned for cache operation [Builder[public com.wewetea.open.weadmin.model.po.User com.wewetea.open.weadmin.service.impl.UserServiceImpl.queryUserById(java.lang.Long)] caches=[caffeineCacheManager] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='']. If you are using named parameters, ensure that the compiler uses the '-parameters' flag.
    at org.springframework.cache.interceptor.CacheAspectSupport.generateKey(CacheAspectSupport.java:764)
    at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.performCachePut(CacheAspectSupport.java:1080)
    at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:1072)
    at org.springframework.cache.interceptor.CacheAspectSupport.evaluate(CacheAspectSupport.java:616)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:448)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:410)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:65)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)

使用Maven添加caffeine作为本地缓存,结果出现以上的错误。

  <!-- 集成本地缓存 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

重要提示:If you are using named parameters, ensure that the compiler uses the '-parameters' flag.
因此,在编译插件是,添加一个参数标志就可以解决了。

 <!-- 编译插件 -->
  <!-- 编译插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <parameters>true</parameters>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • PS:
    关键添加<parameters>true</parameters>,就能解决以上的问题。
    在父级工程的POM文件添加,只要添加一次,其它就都可以用了。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容