Protocol Buffer Java Instance

Source: Internet
Author: User

We need to download the Protobuffer first, here:

Https://code.google.com/p/protobuf/downloads/list

Note that you need to download two, one is complier, the other is source code (i downloaded the 2.5 version);

complier corresponding Protoc.exe copy to source code corresponding to the./PROTOBUF-2.5.0/SRC Directory (mvn compilation required);

CD to./protobuf-2.5.0/java directory, perform mvn clean Package command packaging;

To the target directory to get the generated Protobuf-java-2.5.0.jar package (Java code to PROTOBUF dependent jar package);

Person.proto file

Package com.shine.pb;option Java_package="Com.shine.pb.person"; option Java_outer_classname="MyPerson"; message person {required Int32 ID= 1; Required String Name= 2; Optional String Email= 3; Enum Phonetype {MOBILE=0; FIXED= 1; } message Phoneinfo {Required String number= 1; Required Phonetype Type= 2 [default=MOBILE]; } Optional Phoneinfo phone= 4;}

To the directory where Protoc.exe is located, execute protoc--java_out=./output/java./messages/mymessage.proto

Can generate Java classes corresponding to the proto file;

Copy to your eclipse corresponding to SRC; Write the main method test, the code is as follows:

 PackageCom.shine.pb.person;Importcom.google.protobuf.InvalidProtocolBufferException;ImportCom.shine.pb.person.MyPerson.Person.PhoneType; Public classTest { Public Static voidMain (string[] args)throwsinvalidprotocolbufferexception {MyPerson.Person.Builder Builder=MyPerson.Person.newBuilder (); Builder.setid (1); Builder.setname ("Shine"); Builder.setemail ("[Email protected]"); MyPerson.Person.PhoneInfo.Builder Phoneinfobuilder=MyPerson.Person.PhoneInfo.newBuilder (); Phoneinfobuilder.setnumber ("13899999999");        Phoneinfobuilder.settype (Phonetype.mobile);                Builder.setphone (Phoneinfobuilder.build ()); Myperson.person Shine=Builder.build ();        System.out.println (Shine.getserializedsize ());        System.out.println (Shine.getemail ());        System.out.println (Shine.getname ());                System.out.println (Shine.getphone (). GetNumber ()); System.out.println ("============================================="); byte[] data =Shine.tobytearray (); Builder=MyPerson.Person.newBuilder (); Myperson.person Person=MyPerson.Person.parseFrom (data);        System.out.println (Person.getserializedsize ());        System.out.println (Person.getemail ());        System.out.println (Person.getname ());    System.out.println (Person.getphone (). GetNumber ()); }}

Protocol Buffer Java Instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.