gRPC 对应Java类型

官方指南

如下链接为Protocol Buffers官方指南,请参阅: https://developers.google.com/protocol-buffers/docs/proto3

.proto定义示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
syntax = "proto3";

option java_multiple_files = true;
// 此处cn.enncloud为程序包名,规则详见“详细说明[1]”
package cn.enncloud;
// 此处使用service标签定义服务
service NewsService {
// 此处使用rpc标签定义方法
    rpc add (News) returns (NewsResponse) {
    }
    rpc save (News) returns (NewsResponse) {
    }
    rpc delete (News) returns (NewsResponse) {
    }
    rpc list (News) returns (NewsResponse) {
    }
}
// 此处使用message标签定义方法的入参和出参
message NewsResponse {
    News news = 1 [deprecated = true];
    repeated News newslist = 2;
}
message News {
     int32 id = 1;
     string requestTitle = 2;
}

详细说明:

  • [1]package后面接的cn.enncloud是程序包名,可以自定义;
  • proto文件名必须小写开头,每个单词都要以小写字母开头,单词之间以下划线分割,例如xxx_yyy_service.proto。对应的servcie要定义为XxxYyyService,必须严格驼峰,不能连续大写;
  • 一个proto文件内只能定义一个service,多个 service 需要多个 proto 文件,service和message必须在同一个 proto 文件中;
  • message定义的对象命名规范为首字母大写,如此定义是gPRC的规范;
  • proto 文件须放置在项目中的如下目录${Project}/src/main/proto/

类型对应关系表

注:由于proto本身的限制,建议真正使用BeanCopyUtils时不要建议使用标准类型与包装类型直接转换;例如:int->integer

.proto Type Java Type Desc
doubledouble 
floatfloat 
int32int 
int64long 
uint32int 
uint64long 
sint32int 
sint64long 
fixed32int 
fixed64long 
sfixed32int 
sfixed64long 
boolboolean 
stringString 
bytesByteString示例[2]

示例[2]

1
2
3
4
5
message Simple {
    string msg = 1;
    // repeated string 对字节数组
    repeated string results = 2;
}


.proto Type Notes C++ Type Java Type Python Type[2] Go Type Ruby Type C# Type PHP Type
double doubledoublefloatfloat64Floatdoublefloat
float floatfloatfloatfloat32Floatfloatfloat
int32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.int32intintint32Fixnum or Bignum (as required)intinteger
int64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.int64longint/long[3]int64Bignumlonginteger/string[5]
uint32Uses variable-length encoding.uint32int[1]int/long[3]uint32Fixnum or Bignum (as required)uintinteger
uint64Uses variable-length encoding.uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]
sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.int32intintint32Fixnum or Bignum (as required)intinteger
sint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.int64longint/long[3]int64Bignumlonginteger/string[5]
fixed32Always four bytes. More efficient than uint32 if values are often greater than 228.uint32int[1]intuint32Fixnum or Bignum (as required)uintinteger
fixed64Always eight bytes. More efficient than uint64 if values are often greater than 256.uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]
sfixed32Always four bytes.int32intintint32Fixnum or Bignum (as required)intinteger
sfixed64Always eight bytes.int64longint/long[3]int64Bignumlonginteger/string[5]
bool boolbooleanboolboolTrueClass/FalseClassboolboolean
stringA string must always contain UTF-8 encoded or 7-bit ASCII text.stringStringstr/unicode[4]stringString (UTF-8)stringstring
bytesMay contain any arbitrary sequence of bytes.stringByteStringstr[]byteString (ASCII-8BIT)ByteStringstring

扩展实现示例

枚举
1
2
3
4
5
6
7
8
9
enum Corpus {
    UNIVERSAL = 0;
    WEB = 1;
    IMAGES = 2;
    LOCAL = 3;
    NEWS = 4;
    PRODUCTS = 5;
    VIDEO = 6;
}
对象列表List
1
2
3
4
5
6
7
8
9
10
message SearchResponse {
  // 此处就是定义对象的List
  repeated Result results = 1;
}

message Result {
  string url = 1;
  string title = 2;
  repeated SearchResponse searchResponse = 3;
}
Map
1
2
3
4
5
6
message MeetingResponse {
    string id = 1;
    string error = 2;
    // 此处定义了Map对象
    map<string, string> projects = 3;
}

由于ProtoBuffer不支持Object类型,因此建议如果有需求map<string, string>的方式

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值