Protocol Buffers(protobuf)在Java开发中使用测试

Protocol Buffers(protobuf)在Java开发中使用测试

1、工具准备 proto.exe 版本2.6.1
2、jar包:protobuf-java.jar 版本:2.6.1
1)https://github.com/google/protobuf :用maven在java目录中 编译打包 maven:compile package
2)或使用maven直接配置:


com.google.protobuf
protobuf-java
2.6.1

开始测试

1.编写一个.proto文件命名为:addressbook.proto,该文件内容来自protocal-buffers官网

package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
     required string name = 1;
     required int32 id = 2;
     optional string email = 3;
     enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }
message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
message AddressBook {  
    repeated Person person = 1;
}

2、查询protoc.exe帮助:

F:\pbtest>protoc.exe -h

3、生成Java类:

F:\pbtest>protoc.exe --proto_path=F:\pbtest --java_out=F:\pbtest\test\src F:\pbtest\addressbook.proto

4、把生成的代码导入到工程中

5、编写测试类

import java.util.Arrays;
import com.example.tutorial.AddressBookProtos.AddressBook;
import com.example.tutorial.AddressBookProtos.Person;
import com.google.protobuf.InvalidProtocolBufferException;
public class AddressBookProtoUse {
public static void main(String[] args) {
    // 构建一个Person对象
    Person person = Person.newBuilder().setEmail("[email protected]").setId(10086).setName("zhangsan")
            .addPhone(Person.PhoneNumber.newBuilder().setNumber("186").setType(Person.PhoneType.HOME).build())
            .build();
    System.out.println("打印输出Person对象信息:");
    System.out.println(person);
    System.out.println("Person对象调用toString()方法:");
    System.out.println(person.toString());

    System.out.println("Person对象字段是否初始化:" + person.isInitialized());

    // 序列号
    System.out.println("Person对象调用toByteString()方法:");
    System.out.println(person.toByteString());

    System.out.println("Person对象调用toByteArray()方法:");
    System.out.println(Arrays.toString(person.toByteArray()));

    try {
        System.out.println("反序列化后的对象信息:");
        // 反序列化
        Person newPerson = Person.parseFrom(person.toByteArray());
        System.out.println(newPerson);
        newPerson = Person.parseFrom(person.toByteString());
        System.out.println(newPerson);
    } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
    }

    // 向地址簿添加两条Person信息
    AddressBook.Builder books = AddressBook.newBuilder();
    books.addPerson(person);
    books.addPerson(Person.newBuilder(person).setEmail("[email protected]").build());
    System.out.println("AddressBook对象信息:");
    System.out.println(books.build());

}
}

6、测试结果

 打印输出Person对象信息:
    name: "zhangsan"
    id: 10086
    email: "[email protected]"
    phone {
      number: "186"
      type: HOME
    }

    Person对象调用toString()方法:
    name: "zhangsan"
    id: 10086
    email: "[email protected]"
    phone {
      number: "186"
      type: HOME
    }

    Person对象字段是否初始化:true
    Person对象调用toByteString()方法:
    
    Person对象调用toByteArray()方法:
    [10, 8, 122, 104, 97, 110, 103, 115, 97, 110, 16, -26, 78, 26, 16, 122, 104, 97, 110, 103, 115, 97, 110, 64, 49, 54, 51, 46, 99, 111, 109, 34, 7, 10, 3, 49, 56, 54, 16, 1]
    反序列化后的对象信息:
    name: "zhangsan"
    id: 10086
    email: "[email protected]"
    phone {
      number: "186"
      type: HOME
    }

    name: "zhangsan"
    id: 10086
    email: "[email protected]"
    phone {
      number: "186"
      type: HOME
    }

    AddressBook对象信息:
    person {
      name: "zhangsan"
      id: 10086
      email: "[email protected]"
      phone {
        number: "186"
        type: HOME
      }
    }
    person {
      name: "zhangsan"
      id: 10086
      email: "[email protected]"
      phone {
        number: "186"
        type: HOME
      }
    }

6、探究AddressBookProtos类:

a.构造Person对象,Person类继承自com.google.protobuf.GeneratedMessage类,而GeneratedMessage类继承自AbstractMessage类,并且实现了序列化接口Serializable。在AbstractMessage类中重写了toString()方法,具体内容如下:

@Override
  public final String toString() {
    return TextFormat.printToString(this);
  }

于是有了Person对象调用toString()方法后直接输出Person对象的文本内容。

b.toByteString()返回的ByteString是一个不可变的byte序列,由AbstractMessage类实现。toByteArray()返回byte[]。这两个方法都是对象进行序列化的方法。

c.isInitialized()判断对象的字段是否初始化,该方法与Person类的initFields()方法相关。

initFields()源代码:

private void initFields() {
      name_ = "";
      id_ = 0;
      email_ = "";
      phone_ = java.util.Collections.emptyList();
    }

initFields()方法的调用是在Person类的默认实力对象初始化之后调用的,在Person类的静态代码块中可以看到:

static {
      defaultInstance = new Person(true);
      defaultInstance.initFields();
    }

d.Person类提供了一系列的反序列化的重载方法用来讲数据反序列化为Person对象。

e.关于Person对象的Build

Person类中有一个Builder的内部类,该类用来构建Person对象,并且为Person对象添加数据。

public static final class Builder extends
        com.google.protobuf.GeneratedMessage.Builder<Builder>
       implements com.example.tutorial.AddressBookProtos.PersonOrBuilder

4.通过学习官网的实例,手绘一张Java使用protobuf的基本流程图

你可能感兴趣的:(java)