Reputation: 169
I am using Netty4 to develop a Client/Server application. I need to transfer different types of Java Objects (POJOs) from the client to server and vice-verse. I am bit confused on how the client or the server would know the type of the java object received? Also, is it a good idea to transfer Java objects like this (or) try to use a format like JSON/XML/Proto-buffers and convert the message to Java Object after receiving?
Upvotes: 2
Views: 1402
Reputation: 63955
Objects are more of a concept that exist virtually in memory and you can not transfer them as they are. You have to use some way of serialization.
Serialization results in a stream of data that describes the state and type of your data in a way that all involved sides can understand.
Java comes with a serialization mechanism (Serializable) which does not require much work from your side. The serialized information it produces contains the class name so the other side knows which class is responsible to create objects from the serialized data. Using that mechanism requires that both sides share the same classes (e.g. sharing a common library) and they must both be written in Java (* possible that there are ways around that). It's also a good idea if they have the exact same version of the class. Once you update just one side it may get difficult because that can change the way how state is expressed.
Other serialization mechanisms like JSON, Protocol Buffer, ... are typically independent of your concrete implementation and language. They still contain a description of all the required state but you are not bound to certain classes or even the concept of Objects.
Upvotes: 1
Reputation: 23557
You could use Protobuf or just Serialization. Netty ships decoder/encoder for both o them.
Upvotes: 1