最新互联网项目平台网站,手机销售网站建设项目书,网页布局有哪几种,网站元素优化 移动站在本周的学习中#xff0c;学习了关于Redis中关于对象的序列化的相关操作#xff0c;借助Redis的对对象的序列化操作#xff0c;在此讲讲Java中的序列化和反序列化操作。Redis中对于对象的序列化操作#xff0c;一般借助RedisTemplate以及序列化器#xff08;RedisSeriali… 在本周的学习中学习了关于Redis中关于对象的序列化的相关操作借助Redis的对对象的序列化操作在此讲讲Java中的序列化和反序列化操作。Redis中对于对象的序列化操作一般借助RedisTemplate以及序列化器RedisSerializer来实现Java对象与Redis存储格式的转换而JavaEE中的序列化和反序列化操作通常是借助于IO流实现的。下面分别介绍两者。Java中的序列化和反序列化的操作 1.核心作用在 Java 中序列化Serialization 和 反序列化Deserialization 是用于对象持久化和网络传输的核心机制。序列化将 Java 对象转换为字节流的过程便于存储到文件或通过网络传输。反序列化将字节流恢复为 Java 对象的过程用于读取存储的对象或接收网络传输的数据。 2.序列化用途 ● 持久化将对象保存到文件或数据库中例如缓存会话状态。● 网络传输在分布式系统中传递对象例如远程方法调用RMI。实现条件● 类必须实现 java.io.Serializable 接口标记接口无需实现任何方法。● 所有非 transient 字段必须可序列化基本类型自动支持。关键类与方法● ObjectOutputStream用于序列化对象调用 writeObject() 方法。● ObjectInputStream用于反序列化对象调用 readObject() 方法。3.下面通过代码介绍一下两者的用途import java.io.*;// 1. 定义可序列化的类
class Person implements Serializable {private static final long serialVersionUID 1L; // 版本号可选但推荐private String name;private int age;private transient String password; // transient 字段不会被序列化public Person(String name, int age, String password) {this.name name;this.age age;this.password password;}Overridepublic String toString() {return Person{name name , age age , password password };}
}
public class SerializationExample {public static void main(String[] args) {// 序列化对象到文件try (ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(person.ser))) {Person person new Person(Alice, 30, secret);oos.writeObject(person);System.out.println(对象已序列化);} catch (IOException e) {e.printStackTrace();}// 从文件反序列化对象try (ObjectInputStream ois new ObjectInputStream(new FileInputStream(person.ser))) {Person restoredPerson (Person) ois.readObject();System.out.println(对象已反序列化: restoredPerson);// 输出: Person{nameAlice, age30, passwordnull}} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}}
}4.一些注意事项1 serialVersionUID建议显式声明 serialVersionUID 以确保版本兼容性。若未指定Java 会根据类结构自动生成可能 导致反序列化失败。2transient 关键字标记为 transient 的字段不会被序列化如敏感信息、临时数据。3) 继承与序列化若父类实现 Serializable子类自动支持序列化。若父类未实现子类需自行处理父类状态如通过 readObject()/writeObject() 方法。4 异常处理序列化 / 反序列化过程中可能抛出 IOException 或 ClassNotFoundException。Redis中处理序列化的操作 1.核心组件RedisTemplateSpring 提供 RedisTemplate 简化 Redis 操作默认用 JDK 序列化JdkSerializationRedisSerializer 但存在序列化后数据可读性差、体积大等问题通常需自定义序列化策略常见做法是配置 RedisSerializer 实现类。2.这里介绍一下Json的序列化Jackson以 Jackson2JsonRedisSerializer 为例import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;Configuration
public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateString, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);// key 序列化StringRedisSerializer keySerializer new StringRedisSerializer();redisTemplate.setKeySerializer(keySerializer);redisTemplate.setHashKeySerializer(keySerializer);// value 序列化Jackson JSONJackson2JsonRedisSerializerObject valueSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();// 配置序列化如包含所有字段、支持多态等objectMapper.setVisibility(com.fasterxml.jackson.annotation.PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);objectMapper.activateDefaultTyping(com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);valueSerializer.setObjectMapper(objectMapper);redisTemplate.setValueSerializer(valueSerializer);redisTemplate.setHashValueSerializer(valueSerializer);return redisTemplate;}
}以Json的形式存储可读性强体积相对较小还支持多态等场景反序列化。3.自定义序列化器若对序列化有特殊需求如加密、自定义格式 可实现 RedisSerializer 接口import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.SerializationUtils;public class CustomRedisSerializerT implements RedisSerializerT {Overridepublic byte[] serialize(T t) throws SerializationException {if (t null) {return new byte[0];}// 这里用 Spring 工具类也可自定义序列化逻辑如 JSON 库、ProtoBuf 等return SerializationUtils.serialize(t); }Overridepublic T deserialize(byte[] bytes) throws SerializationException {if (bytes null || bytes.length 0) {return null;}return (T) SerializationUtils.deserialize(bytes);}
} 使用时在RedisTemplate中替换对应序列化器Bean
public RedisTemplateString, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateString, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(redisConnectionFactory);CustomRedisSerializerObject customSerializer new CustomRedisSerializer();redisTemplate.setValueSerializer(customSerializer);// 其他序列化器配置...return redisTemplate;
}以上就是关于序列化和反序列化操作的讲述。
文章转载自: http://www.morning.khlxd.cn.gov.cn.khlxd.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.bfrsr.cn.gov.cn.bfrsr.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.fnbtn.cn.gov.cn.fnbtn.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.fdmtr.cn.gov.cn.fdmtr.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.ahlart.com.gov.cn.ahlart.com http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.nmnhs.cn.gov.cn.nmnhs.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.bnxnq.cn.gov.cn.bnxnq.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.nbfkk.cn.gov.cn.nbfkk.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.bktzr.cn.gov.cn.bktzr.cn http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn