How to serialize a map in Java to store in Redis?

2019-07-29 23:12发布

This question already has an answer here:

I have the following classes:

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

}

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:keyword

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-29 23:55

Try to make Keyword implement java.io.Serializable

查看更多
叛逆
3楼-- · 2019-07-30 00:10

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

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.

Please read this.

查看更多
登录 后发表回答