Jedis SpringBoot无法序列化和反序列化Map <String,Object>

时间:2019-11-22 17:23:49

标签: java spring-boot serialization redis jedis

将@Cacheable批注添加到我的rest方法之一后,出现以下错误:

"status": 500,
"error": "Internal Server Error",
"message": "class java.util.ArrayList cannot be cast to class java.util.Map (java.util.ArrayList and java.util.Map are in module java.base of loader 'bootstrap')",

方法声明为:

@Cacheable("loadDevicesFloors")
@GetMapping("/floors/all-devices")
public Map<String, DevicesFloorDTO> loadDevicesFloors() {...

和DevicesFloorDTO如下所示:

public class DevicesFloorDTO implements Serializable {
    private final List<DeviceDTO> deviceDTOs;
    private final String floorName;
    private final Integer floorIndex;
    public DevicesFloorDTO(List<DeviceDTO> devicesDtos, String floorName, Integer floorIndex) {
        this.deviceDTOs = devicesDtos;
        this.floorName = floorName;
        this.floorIndex = floorIndex;
    }...

另外,我的@Bean redisTemplate方法实现:

@Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConFactory
                = new JedisConnectionFactory();
        jedisConFactory.setHostName(redisHost);
        jedisConFactory.setPort(redisPort);
        jedisConFactory.setPassword(redisPassword);
        return jedisConFactory;
    }

@Bean
    public RedisTemplate<?, ?> redisTemplate() {
        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }

有人知道这个实现有什么问题吗?如果没有@Cacheable,它将按预期运行,但是在添加@Cacheable之后会发生错误。我进行了很多搜索,但仍然不知道是什么原因导致此错误,以及如何解决此问题。任何评论可能会有所帮助。解冻很多!

1 个答案:

答案 0 :(得分:1)

您为Map Map<String, DevicesFloorDTO>指定的泛型在序列化/反序列化期间将在运行时不可用。您尝试将对象保存到Reids的哪种格式?它们是另存为JSON(字符串)还是二进制文件?

我们在GenericJackson2JsonRedisSerializer上取得了成功,因为它将类信息保存在JSON字符串中,因此Redis确切地知道如何重新创建对象。

在某些情况下,需要包装对象才能正确序列化/反序列化对象。


    @Bean
    public RedisCacheManager cacheManager( RedisConnectionFactory redisConnectionFactory,
                                           ResourceLoader resourceLoader ) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
                .builder( redisConnectionFactory )
                .cacheDefaults( determineConfiguration() );
        List<String> cacheNames = this.cacheProperties.getCacheNames();
        if ( !cacheNames.isEmpty() ) {
            builder.initialCacheNames( new LinkedHashSet<>( cacheNames ) );
        }
        return builder.build();
    }

    private RedisCacheConfiguration determineConfiguration() {
        if ( this.redisCacheConfiguration != null ) {
            return this.redisCacheConfiguration;
        }
        CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        ObjectMapper mapper = new Jackson2ObjectMapperBuilder()
                .modulesToInstall( new SimpleModule().addSerializer( new NullValueSerializer( null ) ) )
                .failOnEmptyBeans( false )
                .build();
        mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY );

        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer( mapper );

        //get the mapper b/c they registered some internal modules
        config = config.serializeValuesWith( RedisSerializationContext.SerializationPair.fromSerializer( serializer ) );

        if ( redisProperties.getTimeToLive() != null ) {
            config = config.entryTtl( redisProperties.getTimeToLive() );
        }
        if ( redisProperties.getKeyPrefix() != null ) {
            config = config.prefixKeysWith( redisProperties.getKeyPrefix() );
        }
        if ( !redisProperties.isCacheNullValues() ) {
            config = config.disableCachingNullValues();
        }
        if ( !redisProperties.isUseKeyPrefix() ) {
            config = config.disableKeyPrefix();
            config = config.computePrefixWith( cacheName -> cacheName + "::" );
        }
        return config;
    }