惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Zhang_Xiang

代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering 从 page、page_size 到游标:深入解析C端产品的两种主流分页技术 Apache Kafka 的基本概念 Apache Kafka 移除 ZK Proposals webRTC demo Spring Authorization Server(AS)从 Mysql 中读取客户端、用户 Spring Data JPA 使用 Spring Authorization Server 实现授权中心 OAuth 2.1 框架 Spring Security dapr 本地环境升级 BuildPack 打包 spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD 如何拆分大型单体系统为微服务 高可用 Keycloak,K8s Keycloak 13 自定义用户身份认证流程(User Storage SPI) - Zhang_Xiang OAuth 2.0、OIDC 讲不清楚? Mokito 单元测试与 Spring-Boot 集成测试 关于 JMeter 5.4.1 的一点记录
Java 对象实现 Serializable 的原因
Zhang_Xiang · 2022-05-22 · via 博客园 - Zhang_Xiang

java.io.Serializable 是 Java 中的一种标记接口(marker interface)。标记接口是一种特殊的接口,java.io.Serializable 接口没有任何方法,也没有常量。

对象序列化是将对象转换为静态有序的字节流的过程,因此该对象可以用于传输或持久化。反序列化是该过程的反向操作,即将字节流转换为 Java 对象。Java 对象实现 java.io.Serializable 该接口后,就能实现序列化和反序列化。序列化在远程调用中非常常见。

暴露实体

定义 member 实体如下:

@Data  
public class Mbr implements java.io.Serializable {  
  
    private Long id;  
  
    private String name;  
  
    private String age; 
}

我们在实践中,可能会遇到不定义 dto 对象,直接在接口中暴露实体对象 create(Mbr mbr),update(Mbr mbr) 的情况,这时实体对象必须实现 java.io.Serializable 接口。

当实体实现了 java.io.Serializable 接口后,所有继承它的实体也能被序列化。如果实体里引用了其它对象,那么被引用的对象也应该可以序列化。

@Data  
public class Mbr implements java.io.Serializable {  
  
    private Long id;  
  
    private String name;  
  
    private String age; 

	// Address 应该也实现 java.io.Serializable 接口
	private Address address;
}

serialVersionUID

@Data  
public class Mbr implements java.io.Serializable {  

	private static final long serialVersionUID = 1L;
  
    private Long id;  
  
    private String name;  
  
    private String age; 
}

JVM 通过版本号(serialVersionUID) 将序列化的实体联系起来。在反序列化的过程中,JVM 会将字节流中的 serialVersionUID 与本地响应的实体类中的 serialVersionUID 比较,相同,可以序列化,否则抛出 InvaidCastException