Protocol Buffers(protobuf)在Java开发中使用
来源:互联网 发布:json的datatype 编辑:程序博客网 时间:2024/06/05 20:45
关于protobuf的使用我自己有一篇相关的文章:Google Protobuf .proto文件生成Java代码问题处理
文章地址:http://aiilive.blog.51cto.com/1925756/1563449
数据交换的方式很多比如:XML,JSON,Protobuf。虽然protobuf很流行,并且系出名门,可是没怎么用过。通过阅读google developers里关于protocal-buffers的内容基本可以使用它了。如果你访问该链接https://developers.google.com/protocol-buffers/ 失败的话,肯能需要VPN服务。
语言手册:https://developers.google.com/protocol-buffers/docs/protoProtobuf-Java:https://developers.google.com/protocol-buffers/docs/javatutorial下载地址:https://developers.google.com/protocol-buffers/docs/downloads如果是Windows系统建议下载protoc-2.6.0-win32.zip,这样即可省去编译。官方提供的C++,Python,Java的运行库下载:https://code.google.com/p/protobuf/downloads/list 这里需要注意的是运行库的版本需要和protoc的版本对应。下文是通过使用protocal-buffers官网提供的一个数据格式的例子来熟悉protocal buffer在Java开发中的基本使用方法。
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-2.6.0-win32.zip解压后的protoc.exe生成Java类
查询protoc.exe帮助:
D:\__dev\jar\protobuff>protoc.exe -h
生成Java类:
D:\__dev\jar\protobuff>protoc.exe --proto_path=F:\__eclipse\test\proto --java_out=F:\__eclipse\test\src F:\__eclipse\test\proto\addressbook.proto
说明:
上图中addressbook.proto数据格式文件,AddressBookProtos.java是生成的java类,protobuf-java-2.5.0.jar是Java运行时类库。
3.使用AddressBookProtos类来实现对象的序列号和反序列化,了解Protocol-buffers jar的相关API
代码示例:
package com.example.test;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("zhangsan@163.com") .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("tom@163.com") .build()); System.out.println("AddressBook对象信息:"); System.out.println(books.build()); }}
运行结果:
打印输出Person对象信息:name: "zhangsan"id: 10086email: "zhangsan@163.com"phone { number: "186" type: HOME}Person对象调用toString()方法:name: "zhangsan"id: 10086email: "zhangsan@163.com"phone { number: "186" type: HOME}Person对象字段是否初始化:truePerson对象调用toByteString()方法:<ByteString@89ec59 size=40>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: 10086email: "zhangsan@163.com"phone { number: "186" type: HOME}name: "zhangsan"id: 10086email: "zhangsan@163.com"phone { number: "186" type: HOME}AddressBook对象信息:person { name: "zhangsan" id: 10086 email: "zhangsan@163.com" phone { number: "186" type: HOME }}person { name: "zhangsan" id: 10086 email: "tom@163.com" phone { number: "186" type: HOME }}
探究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(); }
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的基本流程图
- Protocol Buffers(protobuf)在Java开发中使用
- Protocol Buffers(protobuf)在Java开发中使用
- Protocol Buffers(protobuf)在Java开发中使用测试
- Protocol Buffers(protobuf)在Java开发中使用测试
- Protocol Buffers(protobuf)在Java开发中使用
- 在Java中使用Protocol Buffers
- 【Protocol Buffers】在Flash中使用Google Protocol Buffers
- 在Flash中使用Google Protocol Buffers
- 在C#中使用Google Protocol Buffers
- 在 iOS 中使用 Protocol Buffers
- 在C++中使用Protocol Buffers
- 在Android中使用Protocol Buffers
- Java 使用 Protocol Buffers
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C语言中字符串与字符数组的使用
- CRC
- C语言之文件操作
- Ionic框架核心技术—Cordova
- 清理win7系统盘空间
- Protocol Buffers(protobuf)在Java开发中使用
- 字符串查找算法总结(暴力匹配、KMP 算法、Boyer-Moore 算法和 Sunday 算法)
- 60条有名的原则与定理
- redis模糊删除
- Ubuntu 16 安装ssh
- 动态规划--九度.1452.搬教室
- Java笔试面试题整理第一波
- 12C降级到11G
- PHP 构造函数的重载