首页 文章

Spring Data Redis:Deserializiation

提问于
浏览
0

我有一类不同的数据类型 . 其中一个是 IntegerInteger 键和int数组值(它有24个单元格) . 我使用Spring Data将类存储在Redis中,但是当我从Redis获取它时,它会出现以下错误 .

这是 Map :

Map<Integer, int[]> mymap = new Hashmap<>();

这是错误:

org.springframework.data.mapping.MappingException: Problem deserializing 'setterless' property ("mymap"): no way to handle typed deser with setterless yet

是否还有其他方法可以序列化和反序列化 mymap ?或者我应该想到存储这个变量的其他方法?

编辑:

这是我的 class :

private String word;
private int DF;
private boolean NE;
private double mean;
private Map<Integer, Burst> interal = new HashMap<>();
private Map<String, Date> docs = new HashMap<>();
private Map<Integer, int[]> TWF;

这是我的redis配置:

public class redisConfig {
@Primary
@Bean("rediscf1")
JedisConnectionFactory jedisConnectionFactory1() {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
    redisStandaloneConfiguration.setDatabase(0);

    return new JedisConnectionFactory(redisStandaloneConfiguration, new JedisConfig());
}


@Primary
@Bean(name = "redis1")
public RedisTemplate<String, Object> redisTemplate1(@Qualifier("rediscf1") JedisConnectionFactory cf) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(cf);
    return template;
}}

1 回答

  • 1

    我的Redis配置:

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }
    
    @Bean
    public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory cf) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(cf);
        return template;
    }
    

    (我建议将此处的Object更改为要保存的实体类的类)

    接下来我有两节课 . save和TestClass的实体就像你的"Burst"类一样 . 请记住添加所有保存类 implements Serializable .

    public class TestEntity implements Serializable {
        private String id;
        private Map<Integer, TestClass> interal = new HashMap<>();
        private Map<Integer, int[]> TWF;
    
        // getters and setters
    }
    
    public class TestClass implements Serializable {
        private String name;
    
        // getters and setters
    }
    

    并保存数据代码:

    /* Initialize Hash operation*/
        String KEY = "redis-map-key";
        hashOperations = redisTemplate.opsForHash();
    
        /* Fill Entity to save */
        TestEntity testEntity = new TestEntity();
    
        Map<Integer, int[]> mapWithArray = new HashMap<>();
        int[] arr = {1, 5, 8};
        mapWithArray.put(1, arr);
    
        /* Internal class */
        TestClass testClass = new TestClass();
        testClass.setName("Test name");
        Map<Integer, TestClass> internal = new HashMap<>();
        internal.put(99, testClass);
    
        /* Fill final object */
        testEntity.setId("entity-id");
        testEntity.setTWF(mapWithArray);
        testEntity.setInteral(internal);
    
        /* Save entity */
        hashOperations.put(KEY, testEntity.getId(), testEntity);
    
        /* Load entity */
        TestEntity entityLoaded = (TestEntity) hashOperations.get(KEY, testEntity.getId());
    
        System.out.println("Entity ID: " + entityLoaded.getId() + ", entity array: " + entityLoaded.getTWF());
    

    RedisTemplate是自动装配的 .

    所有工作都可以使用你的类中的其他类型(Date,int,boolean)

相关问题