rest控制器方法在springboot应用程序中实现redis缓存后不可访问

vfhzx4xs  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(218)

我使用的是SpringBoot1.5.2。我在springboot项目中实现了redis缓存。我在pom.xml文件中添加了redis依赖项。我的缓存配置文件如下所示:

public class CacheConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port ;

    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private Environment springEnv;

    @Autowired
    private JedisConnectionFactory jedisConnectionFactory;

    @Bean
    @Override
    public CacheResolver cacheResolver() {
        return new PropertyResolvingCacheResolver(cacheManager, springEnv);
    }

    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
        // Defaults
        redisConnectionFactory.setHostName(host);
        redisConnectionFactory.setPort(port);
        redisConnectionFactory.setUsePool(true);
        redisConnectionFactory.getPoolConfig().setMaxIdle(30);
        redisConnectionFactory.getPoolConfig().setMinIdle(10);
        return redisConnectionFactory;
    }

   @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory cf) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(cf);
        redisTemplate.setExposeConnection(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(@Autowired  RedisTemplate redisTemplate) {

        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);

        cacheManager.setUsePrefix(true);
        cacheManager.setDefaultExpiration(600);

        cacheManager.setTransactionAware(true);
        cacheManager.setLoadRemoteCachesOnStartup(true);

        return cacheManager;
    }
}

我的rest控制器方法是:

@Cacheable(value="cacheEg", key="#id")
@RequestMapping(value="/{id}")
public StockModel getValueById(@PathVariable Integer id){
    System.out.println("Stock Id is : "+id);
    return stookRepo.findOne(id);
}

在此函数中添加@cacheable注解之前,返回stockmodel对象。当我添加@cacheable注解时,getvaluebyid方法不可访问,并返回以下错误:
java.lang.classcastexception:java.lang.integer不能转换为org.springframework.data.redis.serializer.stringredisserializer.serialize(stringredisserializer)中的java.lang.string。java:33)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscachekey.serializekeyelement(rediscachekey。java:74) ~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscachekey.getkeybytes(rediscachekey。java:49)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscache$1.doinredis(rediscache。java:176)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscache$1.doinredis(rediscache。java:172)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.core.redistemplate.execute(redistemplate。java:207)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.core.redistemplate.execute(redistemplate。java:169) ~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.core.redistemplate.execute(redistemplate。java:157)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscache.get(rediscache。java:172)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.data.redis.cache.rediscache.get(rediscache。java:133)~[spring-data-redis-1.8.1.发布。jar:na]在org.springframework.cache.transaction.transactionwarecachedecontor.get(transactionwarecachedecontor。java:69)~[spring-context-support-4.3.7.release。jar:4.3.7.release]在org.springframework.cache.interceptor.abstractcacheinvoker.doget(abstractcacheinvoker。java:71)~[spring-context-4.3.7.发布。jar:4.3.7.release]在org.springframework.cache.interceptor.cacheaspectsupport.findincaches(cacheaspectsupport。java:537)~[spring-context-4.3.7.发布。jar:4.3.7.release]在org.springframework.cache.interceptor.cacheaspectsupport.findcacheditem(cacheaspectsupport。java:503)~[spring-context-4.3.7.发布。jar:4.3.7.release]位于org.springframework.cache.interceptor.cacheaspectsupport.execute(cacheaspectsupport)。java:389)~[spring-context-4.3.7.发布。jar:4.3.7.release]在org.springframework.cache.interceptor.cacheaspectsupport.execute(cacheaspectsupport)。java:327)~[spring-context-4.3.7.发布。jar:4.3.7.release]在org.springframework.cache.interceptor.cacheinterceptor.invoke(cacheinterceptor。java:61)~[spring-context-4.3.7.发布。jar:4.3.7.release]在org.springframework.aop.framework.reflectivemethodinvocation.procedue(reflectivemethodinvocation。java:179)~[spring-aop-4.3.7.发布。jar:4.3.7.release]在org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy。java:656)~[spring-aop-4.3.7.发布。jar:4.3.7.release]在com.magnus.simfin.api.customer.rest.branchrest$$enhancerbyspringcglib$$4ad895f9.getbybranchid()~[classes/:na]位于sun.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:1.8.0\u 72]位于sun.reflect.nativemethodaccessorimpl.invoke(未知源)~[na:1.8.0\u 72]位于sun.reflect.delegatingmethodaccessorimpl.invoke(未知源)~[na:1.8.0\u 72]java.lang.reflect.method.invoke(未知源代码)~[na:1.8.0\u 72]org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod)。java:205)~[spring-web-4.3.7.发布。jar:4.3.7.release]在org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod。java:133) ~[spring-web-4.3.7.发布。jar:4.3.7.release]位于org.springframework.web.servlet.mvc.method.annotation.servletinvaccablehandlermethod.invokeandhandle(servletinvaccablehandlermethod)。java:116)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]在org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter。java:827)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]位于org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter。java:738) ~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]位于org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter)。java:85)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]在org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet。java:963) ~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]位于org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet。java:897)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]位于org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet。java:970)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]在org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet。java:861)~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]在javax.servlet.http.httpservlet.service(httpservlet。java:622)~[tomcat-embed-core-8.5.11。jar:8.5.11]位于org.springframework.web.servlet.frameworkservlet.service(frameworkservlet。java:846) ~[spring-webmvc-4.3.7.发布。jar:4.3.7.release]在javax.servlet.http.httpservlet.service(httpservlet。java:729)~[tomcat-embed-core-8.5.11。jar:8.5.11]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:230)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:165)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter。java:52)~[tomcat-embed-websocket-8.5.11。jar:8.5.11]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:192) ~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:165)~[tomcat-embed-core-8.5.11。jar:8.5.11]位于org.springframework.boot.web.filter.applicationcontextheaderfilter.dofilterinternal(applicationcontextheaderfilter)。java:55)~[spring-boot-1.5.2.发布。jar:1.5.2.release]在org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter。java:107)~[spring-web-4.3.7.发布。jar:4.3.7.release]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:192)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:165)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter。java:101)~[spring-web-4.3.7.发布。jar:4.3.7.release]在org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:192)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:165)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.springframework.boot.actuate.trace.webrequesttracefilter.dofilterinternal(webrequesttracefilter。java:108)~[spring-boot-actuator-1.5.2.释放。jar:1.5.2.release]在org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter。java:107)~[spring-web-4.3.7.发布。jar:4.3.7.release]在org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain)。java:192)~[tomcat-embed-core-8.5.11。jar:8.5.11]在org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain。java:165)~[tomcat-embed-core-8.5.11。jar:8.5.11]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题