redis无法反序列化对象

enyaitl3  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(719)

我已经将redis缓存添加到我的项目中,缓存本身也可以工作,但是加载缓存值失败,出现以下异常:

java.lang.ClassCastException: class com.dto.FilterOptionsDto cannot be cast to class 
com.dto.FilterOptionsDto (com.dto.FilterOptionsDto is in unnamed module of loader 'app'; 
com.dto.FilterOptionsDto is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @3d0da857)
at ....

缓存配置

@Configuration
@EnableConfigurationProperties({CacheProperties.class})
public class RedisConfig {

    @Bean
    RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(CacheProperties cacheProperties) {
        return builder -> {
            Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>();
            configurationMap.put(CacheNames.IDP_USER_PROFILES, RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofSeconds(cacheProperties.getIdpUserProfilesTtlSeconds())));
            configurationMap.put(CacheNames.STIBO_STORES_FILTER_OPTIONS, RedisCacheConfiguration.defaultCacheConfig()
                    .entryTtl(Duration.ofSeconds(cacheProperties.getIdpUserProfilesTtlSeconds())));
            builder.withInitialCacheConfigurations(configurationMap);
        };
    }

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

}

缓存ussage

@Override
    @Cacheable(value = CacheNames.STIBO_STORES_FILTER_OPTIONS)
    public FilterOptionsDto getStoresFilterOptions(CountryEnum country, boolean includeClosedStores, boolean includeSingleOptions) {

注: FilterOptionsDto 实现可序列化。我发现序列化的值被保存到redis db中,但是每当spring试图反序列化它时,它就失败了。
类似的问题:在springboot中将redis缓存反序列化为对象时出现问题

vfhzx4xs

vfhzx4xs1#

解决方法很奇怪,但很简单-删除 devtools 附属国。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

相关问题