简体   繁体   中英

How to store a List<Object> type of value in Redis using Spring boot?

I want to store key value pair in Redis template using Spring boot. My key is of type Long and value is actually a List<Object> type. I am trying Redis for the first time and I was able to store the normal <String, String> format. But while trying out the above <Long, List<Object>> it gave me SerializationError . Below is my code sample:

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

}

And this is the error I am getting:

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:

I believe issue is in serializing the above types. I am not sure how to do that in the format I want. Can anyone suggest anything here? Also, I would need to retrieve it back as well while reading from Redis.

RedisTemplate OpsForHash accepts key as instanceof byte[].

Try converting the key and value variables to String - type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM