devcodes
devcodes

Reputation: 1088

Java Serialization with JPA and Rest APIs

As we know Serialization in java ,to quote from a blog here is used

to convert the state of an object into a byte stream, which can be persisted into disk/file or sent over the network to any other running Java virtual machine.

Now considering the second case , to send over a network to another running jvm , if we consider an example of a Rest API , i.e "host:port/path/resource".

Usually I use Spring's @RequestMapping to return the resource model pojo class as ResponseEntity. I do not implement Serializable interface in the model class and all works fine , I get the response of the API in json.

ModelX.java

public class ModelX {

    private int x = 2 ;
    private String xs = "stringx";

    // getters and setters

}

Controller method :

@RequestMapping(value = "/test",method=RequestMethod.POST)
public ResponseEntity<ModelX> getTestModel(@RequestBody ModelX mox){

    ModelX mx = new ModelX();
    mx.setX(mox.getX());
    mx.setXs(mox.getXs());

    return new ResponseEntity<ModelX>(mx, HttpStatus.OK) ;
}

Is it because Spring framework makes it Serializable under the hood with those RestAPI annotations ? If not , how without making it serializable we are able to send over a network .


Just for more thought , Even in the case of persisting the Objects in database , we use @Entity from JPA , now I tested if the instance of any @Entity annotated class IS-A Serializable or not . and it gives false.

@Entity
class Car {

int id ;
String name ;

//getters and setters

}

testMethod -

Car c = new Car();
System.out.println(c instanceof Serializable);

O/p - false

So even when we try to save this object's state in a database , ORM also does some kind of Serialization under the hood ?

Upvotes: 5

Views: 3187

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170755

Serialization is a general notion:

In computer science, in the context of data storage, serialization (or serialisation) is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment).

Serializable is used for a single specific implementation of serialization, which happens to be built into JVM and called "Java serialization". But conversion to JSON, or to whatever protocol database uses for communication are also serialization; they just have nothing to do with Serializable.

are there any specific scenarios or applications (maybe if you are aware of )where sending bytestream is more beneficial

For one, binary formats are smaller than JSON and can be read/written faster. And they can still be cross-platform. But for this you generally would use a different binary format than Java serialization: e.g. Protocol Buffers or Thrift (or many others).

so if the app that sends the data goes down , data will still be available for the other jvm . (won't happen in case of API)

This works perfectly well with JSON as well (or PB or Thrift as above).

Upvotes: 1

Related Questions