java序列化与反序列化操作redis

 笔者在使用 SSM 框架项目部分功能进行测试需要使用到对象的序列化与反序列化

第一种方式:jackson

Demo

package com.dznfit.service;

import com.dznfit.controller.LoginController;
import com.dznfit.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javaConfiguration.RootConfig;
import javaConfiguration.WebConfig;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import the_mass.redis.SerializeUtil;

import java.io.IOException;
import java.util.ArrayList;

public class UserServiceImplTest {
private static Logger logger = Logger.getLogger(UserServiceImplTest.class);

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> login() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException {
    User user </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> User(1, "dz", "123", 1<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">放入容器</span>
    AnnotationConfigApplicationContext context =
            <span style="color: rgba(0, 0, 255, 1)">new</span> AnnotationConfigApplicationContext(RootConfig.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">得到bean</span>
    UserServiceImpl bean = context.getBean(UserServiceImpl.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
     * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
     </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
    ObjectMapper mapper </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectMapper();

    System.out.println(</span>"类名" +<span style="color: rgba(0, 0, 0, 1)"> bean.login((user)).getClass().getName());
    String s </span>=<span style="color: rgba(0, 0, 0, 1)"> mapper.writeValueAsString(bean.login(user));

    System.out.println(</span>"序列化:" +<span style="color: rgba(0, 0, 0, 1)"> s);
    
    System.out.println(</span>"反序列化:" + mapper.readValue(s, User.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">));
}

}

结果:

我们可以看到 jackson 实现类

ObjectMapper 有许多方法
序列化时给个 Object 对象就可以了转成 json 字符串
反序列化也是有很多

 

第二种使用 java 自带序列化

需要在实体类实现 implements Serializable 接口

@Test
    public void Serialization() throws IOException, ClassNotFoundException {
        Jedis jedis = new Jedis();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">java原生序列化</span>
    ByteArrayOutputStream stream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ByteArrayOutputStream();
    ObjectOutputStream oos </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectOutputStream(stream);
    oos.writeObject(</span><span style="color: rgba(0, 0, 255, 1)">new</span> News(1, "sdj", "merry christmas"<span style="color: rgba(0, 0, 0, 1)">));
    oos.writeObject(</span><span style="color: rgba(0, 0, 255, 1)">new</span> News(2, "zdm", "mashangyaoofangjiale"<span style="color: rgba(0, 0, 0, 1)">));

    jedis.set(</span>"news-01"<span style="color: rgba(0, 0, 0, 1)">.getBytes(), stream.toByteArray());
    oos.close();
    System.out.println(</span>"---------"<span style="color: rgba(0, 0, 0, 1)">);

    System.out.println(jedis.get(</span>"news-01"<span style="color: rgba(0, 0, 0, 1)">).getBytes().length);
    System.out.println(jedis.get(</span>"news-01"<span style="color: rgba(0, 0, 0, 1)">.getBytes()).length);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">反序列化
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这里有一个坑在调用jedis.get("news-01").getBytes() --error
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 这样调用的话会改变字节码取的时候就不对了
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">存jedis.set("news-01".getBytes())取所有也是jedis.get("news-01".getBytes());</span>
    ByteArrayInputStream bri = <span style="color: rgba(0, 0, 255, 1)">new</span> ByteArrayInputStream(jedis.get("news-01"<span style="color: rgba(0, 0, 0, 1)">.getBytes()));
    ObjectInputStream outs </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectInputStream(bri);
    Object o </span>=<span style="color: rgba(0, 0, 0, 1)"> outs.readObject();
    Object o1 </span>=<span style="color: rgba(0, 0, 0, 1)"> outs.readObject();
    System.out.println(o);
    System.out.println(o1);
    outs.close();
}</span></pre>

结果