java 生成读取 google protobuf 2.4.1 dome
来源:互联网 发布:成都亦知科技有限公司 编辑:程序博客网 时间:2024/06/16 13:47
1,新建Maven项目名为 protobuf 这是pom.xml的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>protobuf</groupId> <artifactId>protobuf</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>protobuf</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.4.1</version></dependency> </dependencies></project>
2, 生成方法
package com.protobuf.test;import java.io.IOException;/** * protoc.exe * @author ganhaibin * */public class GenerareClass { public static void main(String[] args) { String protoFile = "person-entity.proto"; String path="C:\\Users\\Administrator\\Documents\\GitHub\\protobuf";//项目地址 //-----生产protobuf实体类需要依赖google的protobuf项目支持,这个项目我已经集成在我的dome中,不可缺省 String strCmd =path +"/google/src/protoc.exe -I=./proto --java_out=./src/main/java ./proto/"+ protoFile; //proto文件地址 try { //通过执行cmd命令调用protoc.exe程序 Runtime.getRuntime().exec(strCmd); System.out.println(protoFile+".java 生成成功!"); } catch (IOException e) { e.printStackTrace(); } }}
3,proto文件 对应 person-entity.proto
option java_package = "com.roncoo.education.protobuf";//指定生产在protobuf包下option java_outer_classname = "PersonEntity";//生成的数据访问类的类名 message Person { //自定义实体Person required int32 id = 1;//同上 required string name = 2;//必须字段,在后面的使用中必须为该段设置值 optional string email = 3;//可选字段,在后面的使用中可以自由决定是否为该字段设置值}
4,写入读取google protobuf
package com.protobuf.test;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import com.google.protobuf.InvalidProtocolBufferException;import com.protobuf.test.PersonEntity.Person;public class test { public static void main(String[] args) throws IOException { PersonEntity.Person.Builder personBuilder = PersonEntity.Person.newBuilder(); personBuilder.setId(1); personBuilder.setName("name"); personBuilder.setEmail("xx@163.com"); PersonEntity.Person xxg = personBuilder.build(); // 将数据写到输出流,如网络输出流,这里就用ByteArrayOutputStream来代替 ByteArrayOutputStream output = new ByteArrayOutputStream(); xxg.writeTo(output); // -------------- 分割线:上面是发送方,将数据序列化后发送 --------------- byte[] byteArray = output.toByteArray(); // -------------- 分割线:下面是接收方,将数据接收后反序列化 --------------- // 接收到流并读取,如网络输入流,这里用ByteArrayInputStream来代替 ByteArrayInputStream input = new ByteArrayInputStream(byteArray); // 反序列化 PersonEntity.Person pe = PersonEntity.Person.parseFrom(input); System.out.println("ID:" + pe.getId()); System.out.println("name:" + pe.getName()); System.out.println("email:" + pe.getEmail()); System.out.println("friend:"); }}
生产protobuf实体类需要依赖google的protobuf项目支持,这个项目我已经集成在我的dome中,不可缺省
最好 还是下载我的dome吧
项目dome github地址:https://github.com/gyn781369549/protobuf
项目地址 更改为你本地的地址
运行 protobuf/src/main/java/com/protobuf/test/GenerareClass.java 之后按F5 就可以看见效果
生产在 com.protobuf.test.protobu 包下
protobuf 2.4.1版本号不对应也会读取错误,导致读不出来
阅读全文
1 0
- java 生成读取 google protobuf 2.4.1 dome
- google protobuf java 简单使用
- Google Protobuf Java API详解
- iOS 生成二维码~dome
- Google protobuf(1)-简介
- google protobuf源码分析1
- google protobuf
- Google ProtoBuf
- protobuf (GOOGLE)
- google protobuf
- google protobuf
- 【google protobuf】
- google protobuf
- Google protobuf
- Google protoBuf
- Google Protobuf
- Google protobuf
- Google protobuf for java 小试
- Altium Designer -- 精心总结
- 进程管理API之task_tgid_nr_ns
- table 中 点击某行,获取这一行的数据弹框显示,或进行其他的操作
- J2EE开发模型分析
- css sprites的学习
- java 生成读取 google protobuf 2.4.1 dome
- java中的枚举类和常量类的区别
- 明德扬FPGA-全网公开课-第02期-《算法的verilog实现-fir滤波器》
- 数据库sql查询优化
- mysql日志
- springboot 集成 swagger生成在线文档
- Kafka的配置文件详细描述
- SpringMVC与Struts2区别与比较总结
- Python基础篇之切片