如何在 Java 中序列化地图以存储在 Redis 中? [英] How to serialize a map in Java to store in Redis?

查看:64
本文介绍了如何在 Java 中序列化地图以存储在 Redis 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class Document {

    public String id;
    public String date;
    public Map<String, Keyword> keywords = new HashMap<>();

}

public class Keyword {

    public String word;
    public Map<String, Document> docs = new HashMap<>();

}

我想序列化 keywords HashMap 以便将其保存在 Redis 中.

I want to serialize the keywords HashMap in order to save it in Redis.

我尝试了这个,但它抛出了一个错误:

I tried this but it throws an error:

java.io.NotSerializableException:关键字

java.io.NotSerializableException:keyword

推荐答案

使您的 KeywordDocument 类实现 java.io.Serializable.

Make your Keyword and Document classes implement java.io.Serializable.

您尝试序列化的所有内容都应该实现 java.io.SerializableHashMapString 默认情况下是可序列化的,因此您不需要为他们做任何事.

Everything what you are trying to serialize should implement java.io.Serializable, HashMap and String by default are serializable so you need not to do anything for them.

请阅读这个.

这篇关于如何在 Java 中序列化地图以存储在 Redis 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆