protobuf syntax = "proto3"; message Student { string name = 1; int32 age = 2; repeated string subjects = 3; } protoc --java_out=. student.proto <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.18.0</version> </dependency> gradle implementation 'com.google.protobuf:protobuf-java:3.18.0' import com.example.student.Student; public class App { public static void main(String[] args) { Student student = Student.newBuilder() .setName("John Doe") .setAge(20) .addSubjects("Math") .addSubjects("Science") .build(); byte[] serializedData = student.toByteArray(); Student deserializedStudent = Student.parseFrom(serializedData); System.out.println("Name: " + deserializedStudent.getName()); System.out.println("Age: " + deserializedStudent.getAge()); System.out.println("Subjects: " + deserializedStudent.getSubjectsList()); } }


上一篇:
下一篇:
切换中文