【问题标题】:Spring Data Redis: DeserializiationSpring Data Redis:反序列化
【发布时间】:2018-10-06 17:57:30
【问题描述】:

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

这是地图:

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吗? 或者我应该想其他方法来存储这个变量?

编辑:

这是我的课:

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

【问题讨论】:

  • 你能放一些你的 Redis 配置代码,你尝试保存的类和存储库吗?我已经测试了一个包含数组的实体,我在保存和加载数据方面没有问题。
  • @AdamLesiak 我没有使用任何存储库。
  • @AdamLesiak 你能提供一个你测试过的例子吗?

标签: java redis spring-data spring-data-redis


【解决方案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改成你的实体类的class来保存)

接下来我有两节课。用于保存和 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 是自动装配的。

您的班级中的其他类型(日期、整数、布尔值)也可以正常工作

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2018-09-23
    • 2013-02-25
    • 2018-09-25
    • 2015-08-28
    • 2021-09-13
    相关资源
    最近更新 更多