编写 一个test.proto文件
option java_package = "com.proto"; option java_outer_classname = "FirstProtobuf"; message TestBuf { required int32 ID = 1; required string Url = 2; }
利用protoc.exe 生成java文件:
protoc.exe --java_out=./ test.proto
写个测试类:
package com.test; import java.io.IOException; import com.proto.FirstProtobuf; import com.proto.FirstProtobuf.TestBuf; public class TestProtoBuf { public static void main(String[] args) throws IOException { //序列化过程 //FirstProtobuf是生成类的名字,即proto文件中的java_outer_classname //testBuf是里面某个序列的名字,即proto文件中的message testBuf FirstProtobuf.TestBuf.Builder builder = FirstProtobuf.TestBuf.newBuilder(); builder.setID( 123 ); builder.setUrl("http://www.21cn.com"); TestBuf info = builder.build(); byte[] buf = info.toByteArray(); TestBuf tb = TestBuf.parseFrom(buf); System.out.println( tb.getID()); System.out.println( tb.getUrl()); } }
时间: 2024-11-09 10:39:40