google protobuf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25958497/article/details/78730316

java 生成读取
google protobuf 2.4.1 dome

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("[email protected]");  

            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版本号不对应也会读取错误,导致读不出来

猜你喜欢

转载自blog.csdn.net/qq_25958497/article/details/78730316