Language: Java
Serialization / Messaging
Protobuf was developed by Google to provide a faster and smaller alternative to XML and JSON for serializing structured data. It supports backward and forward compatibility, strong typing, and works across multiple languages. Protobuf is widely used in RPC systems, microservices, data storage, and messaging applications.
Protocol Buffers (Protobuf) is a language-neutral, platform-neutral, and extensible mechanism for serializing structured data. It is used to efficiently encode data for communication between services, storage, or configuration in Java applications.
Add dependency in pom.xml:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.24.3</version>
</dependency>implementation 'com.google.protobuf:protobuf-java:3.24.3'Protobuf allows you to define structured data in `.proto` files, generate Java classes, and serialize/deserialize data efficiently. It supports messages, enums, nested structures, and repeated fields.
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
}Defines a simple `Person` message with `name`, `id`, and `email` fields.
# Using protoc compiler
protoc --java_out=src/main/java person.protoGenerates Java classes from the .proto definition for use in your application.
Person person = Person.newBuilder()
.setName("Alice")
.setId(1)
.setEmail("alice@example.com")
.build();
byte[] data = person.toByteArray();Builds a Protobuf message and serializes it to a byte array for storage or transmission.
Person parsedPerson = Person.parseFrom(data);
System.out.println(parsedPerson.getName());Parses a byte array back into a Protobuf message instance.
message AddressBook {
repeated Person people = 1;
}
AddressBook book = AddressBook.newBuilder()
.addPeople(person)
.build();Shows how to define repeated (list) fields and nested messages in Protobuf.
Person person = Person.newBuilder()
.setName("Bob")
.build();
System.out.println(person.getEmail()); // empty string as defaultOptional fields take default values if not explicitly set.
Use `.proto` version 3 syntax for simplicity and modern features.
Leverage repeated fields instead of lists for efficient storage.
Keep backward and forward compatibility by assigning unique field numbers.
Avoid removing fields; deprecate them instead to maintain compatibility.
Use Protobuf for inter-service communication, storage, or network efficiency.