Метод контроллера отдыха недоступен после реализации Redis Cache в приложении springboot - PullRequest
0 голосов
/ 17 июня 2020

Я использую пружинный ботинок 1.5.2. Я реализовал кеш Redis в своем проекте весенней загрузки. Я добавил зависимости redis в файл pom. xml. Мой файл конфигурации кеша выглядит так:

    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;
    }
}

, а мой метод контроллера отдыха:

@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 не может быть приведен к java .lang.String в org.springframework.data.redis.serializer.StringRedisSerializer.serialize (StringRedisSerializer. java: 33) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework. data.redis.cache.RedisCacheKey.serializeKeyElement (RedisCacheKey. java: 74) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework.data.redis.cache.RedisCacheKey.getKeyBytes (RedisCacheKey. java: 49) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework.data.redis.cache.RedisCache $ 1.doInRedis (RedisCache. java: 176 ) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework.data.redis.cache.RedisCache $ 1.doInRedis (RedisCache. java: 172) ~ [spring-data-redis -1.8.1.RELEASE.jar: na] в org.springframework.data.redis.core.RedisTemplate.execute (RedisTemplate. java: 207) ~ [spring-da ta-redis-1.8.1.RELEASE.jar: na] в org.springframework.data.redis.core.RedisTemplate.execute (RedisTemplate. java: 169) ~ [spring-data-redis-1.8.1.RELEASE .jar: na] в org.springframework.data.redis.core.RedisTemplate.execute (RedisTemplate. java: 157) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework .data.redis.cache.RedisCache.get (RedisCache. java: 172) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework.data.redis.cache.RedisCache. get (RedisCache. java: 133) ~ [spring-data-redis-1.8.1.RELEASE.jar: na] в org.springframework.cache.transaction.TransactionAwareCacheDecorator.get (TransactionAwareCacheDecorator. 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.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.cache.interceptor.CacheAspectSupport.findInCaches (CacheAspectSupport. java: 537) ~ [spring-context-4.3.7.RELEASE.jar: 4.3.7 . RELEASE] в org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem (CacheAspectSupport. java: 503) ~ [spring-context-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.cache. interceptor.CacheAspectSupport.execute (CacheAspectSupport. java: 389) ~ [spring-context-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.cache.interceptor.CacheAspectSupport.execute (CacheAspect. 1031 *: 327) ~ [spring-context-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.cache.interceptor.CacheInterceptor.invoke (CacheInterceptor. java: 61) ~ [spring- context-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (ReflectiveMethodInvocation. java: 179) ~ [spring-aop-4.3.7.RELEASE.jar : 4.3.7.RELEASE] в org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept (CglibAopProxy. java: 656) ~ [spring-aop-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в com.magnus.simfin.api.customer.rest.BranchRest $$ EnhancerBySpringCGLIB $$ 4ad8 95f9.getByBranchId () ~ [classes /: na] at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) ~ [na: 1.8.0_72] at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source) ~ [na: 1.8. 0_72] в sun.reflect.DelegatingMethodAccessorImpl.invoke (Неизвестный источник) ~ [na: 1.8.0_72] в java .lang.reflect.Method.invoke (Неизвестный источник) ~ [na: 1.8.0_72] в org.springframework .web.method.support.InvocableHandlerMethod.doInvoke (InvocableHandlerMethod. java: 205) ~ [spring-web-4.3.7.RELEASE. jar: 4.3.7.RELEASE] в org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest (InvocableHandlerMethod. java: 133) ~ [spring-web-4.3.7.RELEASE.jar: 4.3.7.RELEASE ] в org.springframework.web.servlet. mvc .method.annotation.ServletInvocableHandlerMethod.invokeAndHandle (ServletInvocableHandlerMethod. java: 116) ~ [spring-web mvc -4.3.7.RELEASE.RELEASE .RELEASE] в org.springframework.web.servlet. mvc .method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod (RequestMappingHandlerAdapter. java: 827) ~ [spring-web mvc -4.3.7.RELEASE .7.RELEASE] в org.springframework.web.servlet. mvc .method.annotation.RequestMappingHandlerAdapter.handleInternal (RequestMappingHandlerAdapter. java: 738) ~ [spring-web mvc -4.3.7.RELEASE.jar : 4.3.7.RELEASE] в org.springframework.web.servlet. mvc .method.AbstractHandlerMethodAdapter.handle (AbstractHandlerMethodAdapter. java: 85) ~ [spring-web mvc -4.3.7.RELEASE.jar : 4.3.7.RELEASE] в org.springframework.web.servlet.Dispatc herServlet.doDispatch (DispatcherServlet. java: 963) ~ [spring-web mvc -4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet. java: 897) ~ [spring-web mvc -4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet. java: 970) ~ [spring-web mvc -4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet. java: 861) ~ [spring-web mvc -4.3.7.RELEASE.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-web mvc -4.3.7.RELEASE.jar: 4.3.7.RELEASE] в javax .servlet.http.HttpServlet.service (HttpServlet. java: 729) ~ [tomcat-embed-core-8.5.11.jar: 8.5.11] в организации apache .catalina.core.ApplicationFilterChain.internalDoFilter ( ApplicationFilterChain. java: 230) ~ [кот-е mbed-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] в организации 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. : 55) ~ [spring-boot-1.5.2.RELEASE.jar: 1.5.2.RELEASE] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) ~ [spring-web- 4.3.7.RELEASE.jar: 4.3.7.RELEASE] в org. apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 192) ~ [tomcat-embed-core -8.5.11.jar: 8.5.11] в организации 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.RELEASE.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-actator-1.5.2.RELEASE.jar: 1.5.2.RELEASE] в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter. java: 107) ~ [spring-web-4.3.7.RELEASE.jar: 4.3.7.RELEASE] в организации apache .catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain. java: 192) ~ [tomcat-embed-core-8.5.11.jar: 8.5.11] в орг. apache .catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain. java: 165) ~ [tomcat-embed-core-8.5. 11.jar: 8.5.11]

...