【问题标题】:How to store a List<Object> type of value in Redis using Spring boot?如何使用 Spring Boot 在 Redis 中存储 List<Object> 类型的值?
【发布时间】:2018-11-30 04:50:10
【问题描述】:

我想使用 Spring boot 将键值对存储在 Redis 模板中。我的键是Long 类型,而值实际上是List&lt;Object&gt; 类型。我第一次尝试 Redis,我能够存储正常的 &lt;String, String&gt; 格式。但是在尝试上述&lt;Long, List&lt;Object&gt;&gt; 时,它给了我SerializationError。以下是我的代码示例:

RedisConfig.java

@Bean
public RedisTemplate<Long, List<TransactionObject>> redisTemplate() {
    final RedisTemplate<Long, List<TransactionObject>> template = new RedisTemplate<Long, List<TransactionObject>>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
    return template;
}

RedisMessageRepository.java

@Repository
public class RedisMessageRepository {

private static final String KEY = "NEO4J";

private RedisTemplate<Long, List<TransactionObject>> redisTemplate;

private HashOperations hashOperations;

@Autowired
public RedisMessageRepository(RedisTemplate<Long, List<TransactionObject>> redisTemplate){
    this.redisTemplate = redisTemplate;
}

@PostConstruct
private void init(){
    hashOperations = redisTemplate.opsForHash();
}

public void add(final RedisMessage redisMessage) {
    hashOperations.put(KEY, redisMessage.getId(), redisMessage.getName());
}

public void delete(final Long id) {
    hashOperations.delete(KEY, id);
}

public RedisMessage findMessage(final Long id){
    return (RedisMessage) hashOperations.get(KEY, id);
}

public Map<Long, List<TransactionObject>> findAllMessages(){
    return hashOperations.entries(KEY);
}

}

RedisMessage.java:

public class RedisMessage implements Serializable {

private Long id;
private List<TransactionObject> txObj;

public RedisMessage(Long id, List<TransactionObject> txObj){
    this.id=id;
    this.txObj=txObj;

}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public List<TransactionObject> getName() {
    return txObj;
}

public void setName(List<TransactionObject> txObj) {
    this.txObj = txObj;
}

@Override
public String toString(){
    return "RedisMessage{" + "id=" +id + '\''  + ", username =" + txObj.get(0).getUsername() + "}";
}

}

MessagePublisherImpl.java:

@Service
public class MessagePublisherImpl implements MessagePublisher {

@Autowired
private RedisTemplate<Long, List<TransactionObject>> redisTemplate;
@Autowired
private ChannelTopic topic;
public MessagePublisherImpl() {
}
public MessagePublisherImpl(final RedisTemplate<Long, List<TransactionObject>> redisTemplate, final ChannelTopic topic) {
    this.redisTemplate = redisTemplate;
    this.topic = topic;
}

@Override
public void publish(String message) {
    redisTemplate.convertAndSend(topic.getTopic(), message);
}

}

这是我得到的错误:

Servlet.service() for servlet [dispatcherServlet] in context with path []
 threw exception [Request processing failed; nested exception is 
org.springframework.data.redis.serializer.SerializationException: Cannot 
serialize; nested exception is 
org.springframework.core.serializer.support.SerializationFailedException: Failed
 to serialize object using DefaultSerializer; nested exception is 
java.io.NotSerializableException:

我认为问题在于序列化上述类型。我不确定如何以我想要的格式做到这一点。任何人都可以在这里提出任何建议吗?此外,我还需要在从 Redis 读取时将其取回。

【问题讨论】:

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


    【解决方案1】:

    RedisTemplate OpsForHash 接受 key 作为 instanceof byte[]。

    尝试将键和值变量转换为字符串类型。

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2019-10-31
      • 2020-02-16
      • 2019-05-01
      • 2022-01-17
      • 1970-01-01
      • 2021-09-11
      • 2018-12-16
      • 2020-09-08
      相关资源
      最近更新 更多