笔者在使用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();
}

结果

最新文章

  1. TDR测试原理
  2. 【Swift】TTTAttributedLabel使用小记
  3. 用c#开发微信 系列汇总
  4. EditText html 出现提示 This text field does not specify an inputType or a hint
  5. Multiples of 3 and 5
  6. js传url中文参数乱码问题
  7. 解决CSS小于12px的文字在谷歌浏览器显示问题
  8. SendMessage API
  9. position 为absolute时/float 为right,span为block
  10. PHP 超强过滤函数
  11. C++箴言:理解 new-handler的行为
  12. C#操作Xml:如何定义Xsd文件
  13. python之路: 基础篇
  14. Bellman-Ford&&SPFA
  15. CSS---选择器种类 | 层叠性权重
  16. mySQL 增量备份方案
  17. 迭代器&可迭代对象
  18. easyUI中datebox的格式显示
  19. shuffle过程分析
  20. C#.NET常见问题(FAQ)-如何输出带选项的MessageBox,YESNO

热门文章

  1. Python 实现快递查询
  2. Is there a difference between `==` and `is` in Python?
  3. 多机部署lnmp-1
  4. luogu3651 展翅翱翔之时 (はばたきのとき)[基环树+贪心]
  5. Java实现文件的上传下载(含源代码和jar包)
  6. Java 实现大文件切割并生成多个文件
  7. spring定时任务的配置式与注解式
  8. 【Java-算法】 计算十六进制校验位
  9. while循环与do. . . while循环语句
  10. PHP 字符串索引问题