为什么 org.springframework.http.ResponseEntity 没有实现 Serializable。

huangapple go评论75阅读模式
英文:

Why org.springframework.http.ResponseEntity doesn't implements Serializable

问题

我尝试理解Serializable接口及其实用性。

我现在知道,实现Serializable的对象将被转换为字节流,以便可以在网络上传输或持久化在数据库中。

我的问题是为什么org.springframework.http.ResponseEntity没有实现Serializable接口,因为ResponseEntity是Spring RestController的响应?

附注:我提出了这个问题,我知道也许我完全没有抓住要点,我还没有理解Serializable和ResponseEntity的概念。

英文:

I try to understand Serializable interface and it's utility.

I know now that objects that implement Serializable will be converted to Byte stream so they could be transferred over the network or persisted in database.

My question is why org.springframework.http.ResponseEntity doesn't implements Serializable interface since ResponseEntity is the spring RestController response?

PS: I ask the question and I know that maybe I am totaly missing the point and that I do not yet understand the concept Srializable and ResponseEntity.

答案1

得分: 5

java.io.Serializable 使用一种特定类型的序列化,称为Java序列化。当它作为核心语言平台的一部分引入时,它是一项重大创新(仍具有一些值得尊敬的特性,比如处理引用循环),但它也有一些严重的缺点,其中最大的缺点是它与其他平台(包括人的眼睛)不兼容。

相反,现代应用程序倾向于使用其他序列化机制(主要是JSON,仍然有大量XML,以及一些其他选项,如Avro和Protocol Buffers)。这些机制不使用内置的平台序列化功能,而是使用一个库(比如Jackson)来检查对象并输出序列化形式。这意味着这些对象不需要实现Serializable接口。

ResponseEntity 不实现Serializable 的一个主要原因是这样做需要使其所有内容也都实现Serializable,否则在运行时可能会出现意外的NotSerializableException。由于这没有实际价值,因此最好跳过这一步。

英文:

java.io.Serializable uses a specific kind of serialization known as Java serialization. It was a major innovation when it was introduced as part of the core language platform (and still has some respectable features, like handling reference cycles), but it has some serious disadvantages, the biggest of which is that it's not compatible with other platforms (including the human eyeball).

Instead, modern applications tend to use other serialization mechanisms (primarily JSON, still plenty of XML, and some other options like Avro and Protocol Buffers). These don't use the built-in platform serialization features and instead use a library (such as Jackson) that inspects the objects and outputs the serialized form. This means that such objects have no need of implementing Serializable.

One major reason that ResponseEntity doesn't implement Serializable is that doing so requires all of its contents to also implement Serializable, or a surprise NotSerializableException can happen at runtime. Since there's no useful value, it's better to skip it.

答案2

得分: 2

Java序列化格式用于那些打算在以后转换回Java对象的对象,通常只有在解码时拥有与编码时相同的类时才有效。

浏览器通常不是用Java编写的,也不应了解org.springframework.http.ResponseEntity类的内部工作。它只知道HTTP协议。

因此,ResponseEntity将以HTTP响应格式传输,这与Java序列化格式非常不同。

英文:

The Java Serialization format is for objects that are intended to be converted back to java objects later, and usually only works if you have the same classes while decoding as while encoding.

A browser is usually not written in Java and should not know the inner workings of the org.springframework.http.ResponseEntity class. It only knows the HTTP protocol.

A ResponseEntity will therefor be transferred in the HTTP response format, which is very different from the Java Serialization format.

答案3

得分: 1

请查看Java序列化教程
>将对象序列化意味着将其状态转换为字节流,以便可以将字节流恢复回对象的副本。

序列化如何通过网络发送字节流无关,而是关于如何将Java对象分解为字节,并将该分解恢复回完全相同的Java对象,就像是:

对象 <--> 字节 <--> 对象。

ResponseEntity<T>只不过是一个特殊格式的 String 消息,通过HTTP协议(TCP连接)从一台机器发送到另一台机器,很可能连同Content-Type头一起发送。

接收方(可以是浏览器)接收数据,将数据转换回以Content-Type指定的可理解的表现形式,这就是结果,它不再是Java对象。

英文:

Have a look at Java Tutorial on Serialization
>To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object.

Serialization is not about how to send byte-stream through network, but rather about how to decompose a Java object into bytes, and revert that decomposition back into exactly same Java object, like:

Object <--> Bytes <--> Object.

ResponseEntity<T> is nothing but a specially formatted String message, that gets sent from one machine to another, most likely along with Content-Type header, via HTTP protocol (TCP connection).

Receiver (let it be a browser) receives the data, converts that data back into human-understandable representational format, specified in Content-Type, and that is the result, which is not a Java object anymore.

huangapple
  • 本文由 发表于 2020年10月9日 22:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64282068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定