java序列化与反序列化操作redis
2024-10-19 17:43:34
笔者在使用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();
}
结果
最新文章
- TDR测试原理
- 【Swift】TTTAttributedLabel使用小记
- 用c#开发微信 系列汇总
- EditText html 出现提示 This text field does not specify an inputType or a hint
- Multiples of 3 and 5
- js传url中文参数乱码问题
- 解决CSS小于12px的文字在谷歌浏览器显示问题
- SendMessage API
- position 为absolute时/float 为right,span为block
- PHP 超强过滤函数
- C++箴言:理解 new-handler的行为
- C#操作Xml:如何定义Xsd文件
- python之路: 基础篇
- Bellman-Ford&;&;SPFA
- CSS---选择器种类 | 层叠性权重
- mySQL 增量备份方案
- 迭代器&;可迭代对象
- easyUI中datebox的格式显示
- shuffle过程分析
- C#.NET常见问题(FAQ)-如何输出带选项的MessageBox,YESNO
热门文章
- Python 实现快递查询
- Is there a difference between `==` and `is` in Python?
- 多机部署lnmp-1
- luogu3651 展翅翱翔之时 (はばたきのとき)[基环树+贪心]
- Java实现文件的上传下载(含源代码和jar包)
- Java 实现大文件切割并生成多个文件
- spring定时任务的配置式与注解式
- 【Java-算法】 计算十六进制校验位
- while循环与do. . . while循环语句
- PHP 字符串索引问题