Java 如何存储列表<;对象>;使用Spring boot的Redis中的值类型?

Java 如何存储列表<;对象>;使用Spring boot的Redis中的值类型?,java,spring-boot,redis,spring-data-redis,Java,Spring Boot,Redis,Spring Data Redis,我想使用SpringBoot在Redis模板中存储键值对。我的键是Long类型,值实际上是列表类型。我第一次尝试Redis,我能够存储正常的格式。但是在尝试上面的时,它给了我序列化错误。下面是我的代码示例: RedisConfig.java @Bean public RedisTemplate<Long, List<TransactionObject>> redisTemplate() { final RedisTemplate<Long, List<

我想使用SpringBoot在Redis模板中存储键值对。我的键是
Long
类型,值实际上是
列表
类型。我第一次尝试Redis,我能够存储正常的
格式。但是在尝试上面的
时,它给了我
序列化错误
。下面是我的代码示例:

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

我认为问题在于序列化上述类型。我不知道如何以我想要的格式做到这一点。这里有人能提出什么建议吗?此外,我还需要在从Redis读取时将其检索回来。

RedisTemplate OpsForHash将键作为字节[]的实例接受

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

@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);
}
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() + "}";
}
@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: