zoukankan      html  css  js  c++  java
  • 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
        public void login() throws IOException {
            User user = new User(1, "dz", "123", 1);
            //放入容器
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(RootConfig.class);
            //得到bean
            UserServiceImpl bean = context.getBean(UserServiceImpl.class);
            /**
             * ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
             */
            ObjectMapper mapper = new ObjectMapper();
    
            System.out.println("类名" + bean.login((user)).getClass().getName());
            String s = mapper.writeValueAsString(bean.login(user));
    
            System.out.println("序列化:" + s);
            
            System.out.println("反序列化:" + mapper.readValue(s, User.class));
        }
    }

    结果:

    我们可以看到jackson实现类

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

    第二种使用java自带序列化

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

    @Test
        public void Serialization() throws IOException, ClassNotFoundException {
            Jedis jedis = new Jedis();
    
            //java原生序列化
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(stream);
            oos.writeObject(new News(1, "sdj", "merry christmas"));
            oos.writeObject(new News(2, "zdm", "mashangyaoofangjiale"));
    
            jedis.set("news-01".getBytes(), stream.toByteArray());
            oos.close();
            System.out.println("---------");
    
            System.out.println(jedis.get("news-01").getBytes().length);
            System.out.println(jedis.get("news-01".getBytes()).length);
            //反序列化
            //这里有一个坑在调用jedis.get("news-01").getBytes() --error
            // 这样调用的话会改变字节码取的时候就不对了
            //存jedis.set("news-01".getBytes())取所有也是jedis.get("news-01".getBytes());
            ByteArrayInputStream bri = new ByteArrayInputStream(jedis.get("news-01".getBytes()));
            ObjectInputStream outs = new ObjectInputStream(bri);
            Object o = outs.readObject();
            Object o1 = outs.readObject();
            System.out.println(o);
            System.out.println(o1);
            outs.close();
        }

    结果

  • 相关阅读:
    10个很有用的高级Git命令
    25套用于Web UI设计的免费PSD网页元素模板
    101个MySQL 的调节和优化的提示
    10款最新且超实用的开发框架
    30 个有用的 HTML5 和 CSS3 表单设计
    cetos7最小化安装设置网络启动和更新yum源
    百度地图api开发:根据坐标获得地理描述地址
    防止sql注入的函数addslashes()
    jquery使用ajax提交form表单
    Git常用命令
  • 原文地址:https://www.cnblogs.com/dzcici/p/10183436.html
Copyright © 2011-2022 走看看