Redis serialization reasons and the division of various serialization situations

Serialization

Serialization basic

Insert picture description here

The default use of jdk serialization will make the string escape
Insert picture description here

In actual development, when we want to store objects in redis, they must be serialized.

  1. Of course, if we convert the object to a json string, what is stored at this time is equivalent to a string. Non-serialization does not affect normal operation
  2. However, usually we have to serialize the objects we create.
  3. If we don’t serialize, store the object

Insert picture description here

Insert picture description here

In actual development, we may use json to convert, we don't want to use jdk serialization (the default is jdk serialization). At this point we need to use the configuration class.
We create a redisTemplate object to overwrite the original redis template object in the bean container.

Serialize various situations to distinguish

  1. Case 1: The class is not serialized, and a javabean object is directly stored
    . Result: an error is reported .Insert picture description here

    b. Insert picture description here

    c. Insert picture description here

    d. No serialization, converted to json object storage. The console is normal, the black window is garbled
    i.Insert picture description here

     ii. ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210117120951829.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW5nMTk5MDM4NDgyNTc=,size_16,color_FFFFFF,t_70)
    
     iii. ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210117121005205.png)
    

    e. Serialization and conversion to json object storage. The black window is garbled.
    i.Insert picture description here

  2. Case 2: Class serialization, directly store a javebean object Result: The console is displayed normally, and the black window is garbled
    a.Insert picture description here

    b. Insert picture description here
    Insert picture description here

    c. Insert picture description here

  3. Case 3 uses jackon serialization (custom serialization) The content is output normally.
    a.Insert picture description here

    b. Insert picture description here

  4. Situation 4 When using normally, we usually convert our objects into json storage, and do not directly store a certain java object.
    a. We use the same method as above in use case three.
    b. All objects must be serialized, that is, to achieve

Guess you like

Origin blog.csdn.net/zhang19903848257/article/details/112736097