码迷,mamicode.com
首页 > 编程语言 > 详细

org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

时间:2019-03-30 22:48:20      阅读:684      评论:0      收藏:0      [点我收藏+]

标签:figure   mic   com   set   add   gen   span   man   width   

springboot整合redis时,使用@Cacheable注解,如果方法的key参数为空,就会报org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String的错误。

? 1 错误信息

org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

? 2 如图

技术图片

? 3 解决方案
package com.test.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by toutou on 2019/3/9.
 */
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport {

    @Override
    @Bean
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        // 设置默认过期时间为60秒
        cacheManager.setDefaultExpiration(60);
        return cacheManager;
    }
}

org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

标签:figure   mic   com   set   add   gen   span   man   width   

原文地址:https://www.cnblogs.com/toutou/p/10516440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!